override (C# 參考)--MSDN

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

要擴充或修改繼承的方法、屬性、索引器或事件的抽象實現或虛實現,必須使用 override 修飾符。

 

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 方法具有相同的簽名。 有關繼承的資訊,請參見繼承(C# 編程指南)。

不能重寫非虛方法或靜態方法。 重寫的基方法必須是 virtual、abstract 或 override 的。

override 聲明不能更改 virtual 方法的可訪問性。 override 方法和 virtual 方法必須具有相同的存取層級修飾符。

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

重寫屬性聲明必須指定與繼承屬性完全相同的存取修飾詞、類型和名稱,並且被重寫的屬性必須是 virtual、abstract 或 override 的。

有關如何使用 override 關鍵字的更多資訊,請參見使用 Override 和 New 關鍵字進資料列版本設定(C# 編程指南)和瞭解何時使用 Override 和 New 關鍵字。

此樣本定義了一個名為 Employee 的基類和一個名為 SalesEmployee 的衍生類別。 SalesEmployee 類包括一個額外的屬性 salesbonus,並重寫方法 CalculatePay 以便將該屬性考慮在內。

 

 1 class TestOverride 2 { 3     public class Employee 4     { 5         public string name; 6  7         // Basepay is defined as protected, so that it may be  8         // accessed only by this class and derrived classes. 9         protected decimal basepay;10 11         // Constructor to set the name and basepay values.12         public Employee(string name, decimal basepay)13         {14             this.name = name;15             this.basepay = basepay;16         }17 18         // Declared virtual so it can be overridden.19         public virtual decimal CalculatePay()20         {21             return basepay;22         }23     }24 25     // Derive a new class from Employee.26     public class SalesEmployee : Employee27     {28         // New field that will affect the base pay.29         private decimal salesbonus;30 31         // The constructor calls the base-class version, and32         // initializes the salesbonus field.33         public SalesEmployee(string name, decimal basepay, 34                   decimal salesbonus) : base(name, basepay)35         {36             this.salesbonus = salesbonus;37         }38 39         // Override the CalculatePay method 40         // to take bonus into account.41         public override decimal CalculatePay()42         {43             return basepay + salesbonus;44         }45     }46 47     static void Main()48     {49         // Create some new employees.50         SalesEmployee employee1 = new SalesEmployee("Alice", 51                       1000, 500);52         Employee employee2 = new Employee("Bob", 1200);53 54         Console.WriteLine("Employee4 " + employee1.name + 55                   " earned: " + employee1.CalculatePay());56         Console.WriteLine("Employee4 " + employee2.name + 57                   " earned: " + employee2.CalculatePay());58     }59 }60 /*61     Output:62     Employee4 Alice earned: 150063     Employee4 Bob earned: 120064 */

 

override (C# 參考)--MSDN

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.