abstract(C# 参考)abstract (C# Reference)

abstract 修饰符指示被修改内容的实现已丢失或不完整。The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. abstract 修饰符可用于类、方法、属性、索引和事件。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