C#——关键字:override

C#——关键字:override

扩展或修改继承的方法、属性、索引器或事件的抽象或虚拟实现需要 override 修饰符。

在以下示例中,Square 类必须提供 GetArea 的重写实现,因为 GetArea 继承自抽象 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

override 方法提供从基类继承的方法的新实现。 通过 override 声明重写的方法称为重写基方法。 override 方法必须具有与重写基方法相同的签名。 从 C# 9.0 开始,override 方法支持协变返回类型。 具体而言,override 方法的返回类型可从相应基方法的返回类型派生。 在 C# 8.0 和更早版本中,override 方法和重写基方法的返回类型必须相同。

不能重写非虚方法或静态方法。 重写基方法必须是 virtual、abstract 或 override。

override 声明不能更改 virtual 方法的可访问性。 override 方法和 virtual 方法必须具有相同级别访问修饰符。

不能使用 new、static 或 virtual 修饰符修改 override 方法。

重写属性声明必须指定与继承的属性完全相同的访问修饰符、类型和名称。 从 C# 9.0 开始,只读重写属性支持协变返回类型。 重写属性必须为 virtual、abstract 或 override。

示例

此示例定义一个名为 Employee 的基类和一个名为 SalesEmployee 的派生类。 SalesEmployee 类包含一个额外字段 salesbonus,并且重写方法 CalculatePay 以将它考虑在内。

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($"Employee1 {employee1.name} earned: {employee1.CalculatePay()}");
        Console.WriteLine($"Employee2 {employee2.name} earned: {employee2.CalculatePay()}");
    }
}
/*
    Output:
    Employee1 Alice earned: 1500
    Employee2 Bob earned: 1200
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值