virtual使用关键字修改方法、属性、索引器或事件声明,并允许派生类重写它。
C# 语言参考记录了 C# 语言的最新发布版本。 它还包含即将发布的语言版本公共预览版中功能的初始文档。
本文档标识了在语言的最后三个版本或当前公共预览版中首次引入的任何功能。
小窍门
若要查找 C# 中首次引入功能时,请参阅 有关 C# 语言版本历史记录的文章。
例如,继承此方法的任何类都可以重写它:
public virtual double Area()
{
return x * y;
}
派生类中的 重写成员 可以更改虚拟成员的实现。 有关如何使用 virtual 关键字的详细信息,请参阅使用 Override 和 New 关键字进行版本控制和了解何时使用 Override 和 New 关键字。
调用虚拟方法时,运行时会检查对象的类型是否为重写成员。 它调用最派生类中的重写成员。 如果没有派生类替代该成员,则调用原始成员。
默认情况下,方法是非虚拟的。 不能重写非虚拟方法。
以下示例显示了虚拟属性:
class MyBaseClass
{
// virtual automatically implemented property. Overrides can only
// provide specialized behavior if they implement get and set accessors.
public virtual string Name { get; set; }
// ordinary virtual property with backing field
private int _num;
public virtual int Number
{
get { return _num; }
set { _num = value; }
}
}
class MyDerivedClass : MyBaseClass
{
private string _name;
// Override automatically implemented property with ordinary property
// to provide specialized accessor behavior.
public override string Name
{
get
{
return _name;
}
set
{
if (!string.IsNullOrEmpty(value))
{
_name = value;
}
else
{
_name = "Unknown";
}
}
}
}
除声明和调用语法不同外,虚拟属性的行为与虚拟方法相似。
- 通过包括使用
override修饰符的属性声明,可在派生类中替代虚拟继承属性。
示例
在此示例中,类Shape包含两个坐标x和yArea()虚拟方法。 不同的形状类(如 Circle、Cylinder 和 Sphere)继承 Shape 类,并为每个图形计算表面积。 每个派生类都有各自的 Area() 替代实现。
继承的CircleCylinder类以及Sphere所有使用初始化基类的构造函数,如以下声明所示。
public Cylinder(double r, double h): base(r, h) {}
根据与方法关联的对象,下面的程序通过调用 Area() 方法的相应实现来计算并显示每个对象的相应区域。
class TestClass
{
public class Shape
{
public const double PI = Math.PI;
protected double _x, _y;
public Shape()
{
}
public Shape(double x, double y)
{
_x = x;
_y = y;
}
public virtual double Area()
{
return _x * _y;
}
}
public class Circle : Shape
{
public Circle(double r) : base(r, 0)
{
}
public override double Area()
{
return PI * _x * _x;
}
}
public class Sphere : Shape
{
public Sphere(double r) : base(r, 0)
{
}
public override double Area()
{
return 4 * PI * _x * _x;
}
}
public class Cylinder : Shape
{
public Cylinder(double r, double h) : base(r, h)
{
}
public override double Area()
{
return 2 * PI * _x * _x + 2 * PI * _x * _y;
}
}
static void Main()
{
double r = 3.0, h = 5.0;
Shape c = new Circle(r);
Shape s = new Sphere(r);
Shape l = new Cylinder(r, h);
// Display results.
Console.WriteLine($"Area of Circle = {c.Area():F2}");
Console.WriteLine($"Area of Sphere = {s.Area():F2}");
Console.WriteLine($"Area of Cylinder = {l.Area():F2}");
}
}
/*
Output:
Area of Circle = 28.27
Area of Sphere = 113.10
Area of Cylinder = 150.80
*/
C# 语言规范
有关详细信息,请参阅 C# 语言规范。 该语言规范是 C# 语法和用法的权威资料。