如何:继承类 (Visual C#)

更新:2007 年 11 月

此示例定义 Circle 和 Rectangle 类(它们均继承自 Shape 类),以及 Square 类(继承自 Rectangle 类)。

示例

public class Shape
{
    // Definitions of properties, methods, fields, and events.
}
public class Circle : Shape
{
    // Specialized properties, methods, fields, events for Circle.
}
public class Rectangle : Shape
{
    // Specialized properties, methods, fields, events for Rectangle.
}
public class Square : Rectangle
{
    // Specialized properties, methods, fields, events for Square.
}

编译代码

  • 启动新的控制台应用程序。

  • 将代码复制并粘贴至紧挨 Class1 声明的前面或后面。

可靠编程

请确保您想要继承的类不是 sealed 的。

请参见

概念

C# 语言入门

其他资源

Visual C# 速成版