抽象类归纳

抽象类归纳一-----关键字(abstract)

可以将类声明为抽象类。方法是在类定义中将关键字 abstract 置于关键字 class 的前面。例如:
public abstract class A
{
    // Class members here.
}

 

抽象类不能实例化。抽象类的用途是提供多个派生类可共享的基类的公共定义。例如,类库可以定义一个作为其多个函数的参数的抽象类,并要求程序员使用该库通过创建派生类来提供自己的类实现。

抽象类也可以定义抽象方法。方法是将关键字 abstract 添加到方法的返回类型的前面。例如:
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.
    }
}

 

如果将虚方法声明为抽象方法,则它对于从抽象类继承的所有类而言仍然是虚的。继承抽象方法的类无法访问该方法的原始实现。在前面的示例中,类 F 上的 DoWork 无法调用类 D 上的 DoWork。在此情况下,抽象类可以强制派生类为虚方法提供新的方法实现。

密封类和类成员
可以将类声明为密封类。方法是在类定义中将关键字 sealed 置于关键字 class 的前面。例如:
public sealed class D
{
    // Class members here.
}

 

密封类不能用作基类。因此,它也不能是抽象类。密封类主要用于防止派生。由于密封类从不用作基类,所以有些运行时优化可以使对密封类成员的调用略快。

在对基类的虚成员进行重写的派生类上的类成员、方法、字段、属性或事件可以将该成员声明为密封成员。在用于以后的派生类时,这将取消成员的虚效果。方法是在类成员声明中将 sealed 关键字置于 override 关键字的前面。例如:
public class D : C
{
    public sealed override void DoWork() { }
}

-----------

抽象类归纳二-----定义抽象属性: 关键字(abstract)

下面的示例演示如何定义抽象属性。抽象属性声明不提供属性访问器的实现,它只声明该类支持属性,而将访问器实现留给派生类。下面的示例演示如何实现从基类继承的抽象属性。

此示例由三个文件组成,其中每个文件都单独编译,产生的程序集由下一次编译引用:

abstractshape.cs:包含抽象 Area 属性的 Shape 类。

shapes.cs:Shape 类的子类。

shapetest.cs:测试程序,它显示某些 Shape 派生对象的面积。

若要编译该示例,请使用以下命令:

csc abstractshape.cs shapes.cs shapetest.cs

这样将生成可执行文件 shapetest.exe。

示例
该文件声明的 Shape 类包含 double 类型的 Area 属性。
// compile with: csc /target:library abstractshape.cs
public abstract class Shape
{
    private string m_id;

    public Shape(string s)
    {
        // calling the set accessor of the Id property.
        Id = s;
    }

    public string Id
    {
        get
        {
            return m_id;
        }

        set
        {
            m_id = 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 访问器可用,因此该属性是只读的。

下面的代码演示 Shape 的三个子类,并演示它们如何重写 Area 属性来提供自己的实现。
// compile with: csc /target:library /reference:abstractshape.dll shapes.cs
public class Square : Shape
{
    private int m_side;

    public Square(int side, string id)
        : base(id)
    {
        m_side = side;
    }

    public override double Area
    {
        get
        {
            // Given the side, return the area of a square:
            return m_side * m_side;
        }
    }
}

public class Circle : Shape
{
    private int m_radius;

    public Circle(int radius, string id)
        : base(id)
    {
        m_radius = radius;
    }

    public override double Area
    {
        get
        {
            // Given the radius, return the area of a circle:
            return m_radius * m_radius * System.Math.PI;
        }
    }
}

public class Rectangle : Shape
{
    private int m_width;
    private int m_height;

    public Rectangle(int width, int height, string id)
        : base(id)
    {
        m_width = width;
        m_height = height;
    }

    public override double Area
    {
        get
        {
            // Given the width and height, return the area of a rectangle:
            return m_width * m_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.

posted @ 2006-05-29 09:10  泡面 @ 幸福  阅读(364)  评论(0)    收藏  举报