深入解析C#中的abstract抽象类
抽象类和类成员 public abstract class A { // Class members here. } 抽象类不能实例化。抽象类的用途是提供一个可供多个派生类共享的通用基类定义。例如,类库可以定义一个抽象类,将其用作多个类库函数的参数,并要求使用该库的程序员通过创建派生类来提供自己的类实现。 public abstract class A { public abstract void DoWork(int i); } 抽象方法没有实现,所以方法定义后面是分号,而不是常规的方法块。抽象类的派生类必须实现所有抽象方法。当抽象类从基类继承虚方法时,抽象类可以使用抽象方法重写该虚方法。例如: // compile with: /target:library public class D { public virtual void DoWork(int i) { // Original implementation. } } public abstract class E : D { public abstract override void DoWork(int i); } public class F : E { public override void DoWork(int i) { // New implementation. } } 如果将 virtual 方法声明为 abstract,则该方法对于从抽象类继承的所有类而言仍然是虚方法。继承一个抽象方法的类不能访问该方法的原始实现。在上一个示例中,类 F 中的 DoWork 不能调用类 D 中的 DoWork。通过这种方式,抽象类可以强制派生类为虚方法提供新的方法实现。 定义抽象属性 下面的示例演示如何定义抽象属性。抽象属性声明不提供属性访问器的实现,它只声明该类支持属性,而将访问器实现留给派生类。下面的示例演示如何实现从基类继承的抽象属性。
若要编译该示例,请使用以下命令: csc abstractshape.cs shapes.cs shapetest.cs 这样将生成可执行文件 shapetest.exe。 // compile with: csc /target:library abstractshape.cs public abstract class Shape { private string name; public Shape(string s) { // calling the set accessor of the Id property. Id = s; } public string Id { get { return name; } set { name = value; } } // Area is a read-only property - only a get accessor is needed: public abstract double Area { get; } public override string ToString() { return Id + " Area = " + string.Format("{0:F2}",Area); } } 属性的修饰符就放置在属性声明中。例如: public abstract double Area 声明抽象属性时(如本示例中的 Area),指明哪些属性访问器可用即可,不要实现它们。在此示例中,只有一个 get 访问器可用,因此该属性是只读的。 // compile with: csc /target:library /reference:abstractshape.dll shapes.cs public class Square : Shape { private int side; public Square(int side,string id) : base(id) { this.side = side; } public override double Area { get { // Given the side,return the area of a square: return side * side; } } } public class Circle : Shape { private int radius; public Circle(int radius,string id) : base(id) { this.radius = radius; } public override double Area { get { // Given the radius,return the area of a circle: return radius * radius * System.Math.PI; } } } public class Rectangle : Shape { private int width; private int height; public Rectangle(int width,int height,string id) : base(id) { this.width = width; this.height = height; } public override double Area { get { // Given the width and height,return the area of a rectangle: return width * height; } } } 下面的代码演示一个测试程序,它创建若干 Shape 派生对象,并输出它们的面积。 // compile with: csc /reference:abstractshape.dll;shapes.dll shapetest.cs class TestClass { static void Main() { Shape[] shapes = { new Square(5,"Square #1"),new Circle(3,"Circle #1"),new Rectangle( 4,5,"Rectangle #1") }; System.Console.WriteLine("Shapes Collection"); foreach (Shape s in shapes) { System.Console.WriteLine(s); } } } 输出: Shapes Collection Square #1 Area = 25.00 Circle #1 Area = 28.27 Rectangle #1 Area = 20.00 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |