override(C# 参考)override (C# Reference)

扩展或修改继承的方法、属性、索引器或事件的抽象或虚拟实现需要 override 修饰符。The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

示例Example

在此示例中,Square 类必须提供 GetArea 的重写实现,因为 GetArea 继承自抽象 Shape 类:In this example, the Square class must provide an overridden implementation of GetArea because GetArea is inherited from the abstract Shape class:

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

override 方法提供从基类继承的成员的新实现。An override method provides a new implementation of a member that is inherited from a base class. 通过 override 声明重写的方法称为重写基方法。The method that is overridden by an override declaration is known as the overridden base method. 重写基方法必须具有与 override 方法相同的签名。The overridden base method must have the same signature as the override method. 有关继承的信息,请参阅继承For information about inheritance, see Inheritance.

不能重写非虚方法或静态方法。You cannot override a non-virtual or static method. 重写基方法必须是 virtualabstractoverrideThe overridden base method must be virtual, abstract, or override.

override 声明不能更改 virtual 方法的可访问性。An override declaration cannot change the accessibility of the virtual method. override 方法和 virtual 方法必须具有相同级别访问修饰符Both the override method and the virtual method must have the same access level modifier.

不能使用 newstaticvirtual 修饰符修改 override 方法。You cannot use the new, static, or virtual modifiers to modify an override method.

重写属性声明必须指定与继承的属性完全相同的访问修饰符、类型和名称,并且重写的属性必须是 virtualabstractoverrideAn overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

有关如何使用 override 关键字的详细信息,请参阅使用 Override 和 New 关键字进行版本控制了解何时使用 Override 和 New 关键字For more information about how to use the override keyword, see Versioning with the Override and New Keywords and Knowing when to use Override and New Keywords.

示例Example

此示例定义一个名为 Employee 的基类和一个名为 SalesEmployee 的派生类。This example defines a base class named Employee, and a derived class named SalesEmployee. SalesEmployee 类包含一个额外字段 salesbonus,并且重写方法 CalculatePay 以将它考虑在内。The SalesEmployee class includes an extra field, salesbonus, and overrides the method CalculatePay in order to take it into account.

class TestOverride
{
    public class Employee
    {
        public string name;

        // Basepay is defined as protected, so that it may be 
        // accessed only by this class and derived classes.
        protected decimal basepay;

        // Constructor to set the name and basepay values.
        public Employee(string name, decimal basepay)
        {
            this.name = name;
            this.basepay = basepay;
        }

        // Declared virtual so it can be overridden.
        public virtual decimal CalculatePay()
        {
            return basepay;
        }
    }

    // Derive a new class from Employee.
    public class SalesEmployee : Employee
    {
        // New field that will affect the base pay.
        private decimal salesbonus;

        // The constructor calls the base-class version, and
        // initializes the salesbonus field.
        public SalesEmployee(string name, decimal basepay, 
                  decimal salesbonus) : base(name, basepay)
        {
            this.salesbonus = salesbonus;
        }

        // Override the CalculatePay method 
        // to take bonus into account.
        public override decimal CalculatePay()
        {
            return basepay + salesbonus;
        }
    }

    static void Main()
    {
        // Create some new employees.
        var employee1 = new SalesEmployee("Alice", 
                      1000, 500);
        var employee2 = new Employee("Bob", 1200);

        Console.WriteLine("Employee4 " + employee1.name + 
                  " earned: " + employee1.CalculatePay());
        Console.WriteLine("Employee4 " + employee2.name + 
                  " earned: " + employee2.CalculatePay());
    }
}
/*
    Output:
    Employee4 Alice earned: 1500
    Employee4 Bob earned: 1200
*/

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