• override 修饰符


    override(C# 参考)

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

    C#

    abstract class ShapesClass
    {
        abstract public int Area();
    }
    class Square : ShapesClass
    {
        int side = 0;
    
        public Square(int n)
        {
            side = n;
        }
        // Area method is required to avoid
        // a compile-time error.
        public override int Area()
        {
            return side * side;
        }
    
        static void Main() 
        {
            Square sq = new Square(12);
            Console.WriteLine("Area of the square = {0}", sq.Area());
        }
    
        interface I
        {
            void M();
        }
        abstract class C : I
        {
            public abstract void M();
        }
    
    }
    // Output: Area of the square = 144

     

    override 方法提供从基类继承的成员的新实现。 由 override 声明重写的方法称为重写基方法。 重写的基方法必须与 override 方法具有相同的签名。
    不能重写非虚方法或静态方法。 重写的基方法必须是 virtual、abstract 或 override 修饰的。
    override 声明不能更改 virtual 方法的可访问性。 override 方法和 virtual 方法必须具有相同的访问级别修饰符。
    您不能使用 new、static 或 virtual 修饰符来修改 override 方法。
    重写属性声明必须指定与继承属性完全相同的访问修饰符、类型和名称,并且被重写的属性必须是 virtual、abstract 或 override 修饰的。

    此示例定义了一个名为 Employee 的基类和一个名为 SalesEmployee 的派生类。 SalesEmployee 类包括一个额外的属性 salesbonus,并重写方法 CalculatePay 以便将该属性考虑在内。
    C#

     

    class TestOverride
    {
        public class Employee
        {
            public string name;
    
            // Basepay is defined as protected, so that it may be 
            // accessed only by this class and derrived 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.
            SalesEmployee employee1 = new SalesEmployee("Alice", 
                          1000, 500);
            Employee 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
    */

     

  • 相关阅读:
    程序的了解
    Oracle VM VirtualBox虚拟网卡消失解决方法
    YARN 运维、巡检、监控、调优、排障
    HDFS巡检、监控、调优、排障
    Windows CMD命令大全
    [HDU]6356 Glad You Came(ST表)
    [BZOJ] 1019 [SHOI2008]汉诺塔
    树上叶子之间点对距离平方和
    [BZOJ]1026[SCOI2009]windy数
    [计蒜客]A1542 The Maximum Unreachable Node Set
  • 原文地址:https://www.cnblogs.com/gsk99/p/4975296.html
Copyright © 2020-2023  润新知