共用方式為


如何:定義抽象屬性 (C# 程式設計手冊)

下列範例說明如何定義 abstract 屬性。 抽象屬性宣告不會提供屬性存取子的實作,它會宣告類別支援屬性,但存取子實作則會交由衍生類別 (Derived Class) 處理。 下列範例示範如何實作繼承自基底類別的抽象屬性。

這個範例有三個檔案,每個都是單獨編譯,而且所產生的組件會供接下來的編譯參考:

  • 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 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 存取子是可用的,所以屬性為唯讀。

下列程式碼顯示 Shape 的三個子類別,以及這些子類別如何覆寫 Area 屬性以提供本身的實作。

// 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);
        }
    }
}
/* Output:
    Shapes Collection
    Square #1 Area = 25.00
    Circle #1 Area = 28.27
    Rectangle #1 Area = 20.00
*/

請參閱

工作

如何:使用命令列建立和使用組件 (C# 和 Visual Basic)

參考

類別和結構 (C# 程式設計手冊)

抽象和密封類別以及類別成員 (C# 程式設計手冊)

屬性 (C# 程式設計手冊)

概念

C# 程式設計手冊