abstract (C# 參考)abstract (C# Reference)

abstract 修飾詞表示要修改的項目具有遺失或不完整的實作。The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. 抽象修飾詞可以與類別、方法、屬性、索引子和事件搭配使用。The abstract modifier can be used with classes, methods, properties, indexers, and events. 在類別宣告中使用 abstract 修飾詞,來表示某一類別只是要作為其他類別的基底類別,不是自行具現化。Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes, not instantiated on its own. 標記為抽象的成員,必須由衍生自抽象類別的非抽象類別實作。Members marked as abstract must be implemented by non-abstract classes that derive from the abstract class.

範例Example

在此範例中,Square 類別必須提供 GetArea 的實作,因為它繼承自 ShapeIn this example, the class Square must provide an implementation of GetArea because it derives from Shape:

abstract class Shape
{
    public abstract int GetArea();
}

class Square : Shape
{
    int side;

    public Square(int n) => side = n;

    // GetArea method is required to avoid a compile-time error.
    public override int GetArea() => side * side;

    static void Main() 
    {
        var sq = new Square(12);
        Console.WriteLine($"Area of the square = {sq.GetArea()}");
    }
}
// Output: Area of the square = 144

抽象類別具有下列功能:Abstract classes have the following features:

  • 抽象類別無法具現化。An abstract class cannot be instantiated.

  • 抽象類別可能包含抽象方法和存取子。An abstract class may contain abstract methods and accessors.

  • 因為兩個修飾詞的意義相反,所以無法使用 sealed 修飾詞來修改抽象類別。It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. sealed 修飾詞可防止繼承類別,而 abstract 修飾詞需要繼承類別。The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.

  • 衍生自抽象類別的非抽象類別必須包括所有繼承抽象方法和存取子的實際實作。A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

在方法或屬性宣告中使用 abstract 修飾詞,表示方法或屬性未包含實作。Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.

抽象方法具有下列功能:Abstract methods have the following features:

  • 抽象方法隱含為虛擬方法。An abstract method is implicitly a virtual method.

  • 只有在抽象類別中才允許抽象方法宣告。Abstract method declarations are only permitted in abstract classes.

  • 因為抽象方法宣告未提供實際實作,所以沒有方法主體;方法宣告的結尾就是分號,而且簽章後面沒有大括號 ({ })。Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no curly braces ({ }) following the signature. 例如:For example:

    public abstract void MyMethod();  
    

    方法 override 提供了實作,而這個方法是非抽象類別的成員。The implementation is provided by a method override, which is a member of a non-abstract class.

  • 在抽象方法宣告中使用 staticvirtual 修飾詞是錯誤的。It is an error to use the static or virtual modifiers in an abstract method declaration.

抽象屬性的行為類似抽象方法,但宣告和引動過程語法的差異除外。Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.

  • 在靜態屬性上使用 abstract 修飾詞是錯誤的。It is an error to use the abstract modifier on a static property.

  • 包括使用 override 修飾詞的屬性宣告,即可覆寫衍生類別中的抽象繼承屬性。An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

如需抽象類別的詳細資訊,請參閱抽象和密封類別以及類別成員For more information about abstract classes, see Abstract and Sealed Classes and Class Members.

抽象類別必須提供所有介面成員的實作。An abstract class must provide implementation for all interface members.

實作介面的抽象類別可能會將介面方法對應至抽象方法。An abstract class that implements an interface might map the interface methods onto abstract methods. 例如:For example:

interface I
{
    void M();
}
abstract class C : I
{
    public abstract void M();
}

範例Example

在此範例中,DerivedClass 類別衍生自抽象類別 BaseClassIn this example, the class DerivedClass is derived from an abstract class BaseClass. 抽象類別包含抽象方法 AbstractMethod 和兩個抽象屬性:XYThe abstract class contains an abstract method, AbstractMethod, and two abstract properties, X and Y.

abstract class BaseClass   // Abstract class
{
    protected int _x = 100;
    protected int _y = 150;
    public abstract void AbstractMethod();   // Abstract method
    public abstract int X    { get; }
    public abstract int Y    { get; }
}

class DerivedClass : BaseClass
{
    public override void AbstractMethod()
    {
        _x++;
        _y++;
    }

    public override int X   // overriding property
    {
        get
        {
            return _x + 10;
        }
    }

    public override int Y   // overriding property
    {
        get
        {
            return _y + 10;
        }
    }

    static void Main()
    {
        var o = new DerivedClass();
        o.AbstractMethod();
        Console.WriteLine($"x = {o.X}, y = {o.Y}");
    }
}
// Output: x = 111, y = 161

在上述範例中,如果您嘗試使用如下的陳述式來具現化抽象類別︰In the preceding example, if you attempt to instantiate the abstract class by using a statement like this:

BaseClass bc = new BaseClass();   // Error  

您會收到錯誤,指出編譯器無法建立抽象類別 'BaseClass' 的執行個體。You will get an error saying that the compiler cannot create an instance of the abstract class 'BaseClass'.

C# 語言規格C# Language Specification

如需詳細資訊,請參閱<C# 語言規格>。For more information, see the C# Language Specification. 語言規格是 C# 語法及用法的限定來源。The language specification is the definitive source for C# syntax and usage.

請參閱See also