virtual 修飾符 C# .NET

來源:互聯網
上載者:User

標籤:

virtual

關鍵字用於修飾方法、屬性、索引器或事件聲明,並且允許在衍生類別中重寫這些對象。

例如,此方法可被任何繼承它的類重寫。

(C#參考) 

1 public virtual double Area() 2 3 { 4 5     return x * y;6 7 } 

虛擬成員的實現可由衍生類別中的重寫成員更改 

調用虛方法時,將為重寫成員檢查該對象的運行時類型。將調用大部指派生類中的該重寫成員,

如果沒有衍生類別重寫該成員,則它可能是原始成員。 

預設情況下,方法是非虛擬。不能重寫非虛方法。 

virtual修飾符不能與static、abstract, private或override修飾符一起使用。除了聲明和調用文法不同外,虛擬屬性的行為與抽象方法一樣。在靜態屬性上使用virtual修飾符是錯誤的。 通過包括使用override修飾符的屬性聲明,可在衍生類別中重寫虛擬繼承屬性。 

樣本 

在該樣本中,Dimensions類包含x和y兩個座標和Area()虛方法。不同的形狀類,如Circle、Cylinder和Sphere繼承Dimensions類,並為每個圖形計算表面積。每個衍生類別都有各自的Area()重寫實現。根據與此方法關聯的對象,通過調用正確的Area()實現,該程式為每個圖形計算並顯示正確的面積。 

在前面的樣本中,注意繼承的類Circle、Sphere和Cylinder都使用了初始化基類的建構函式,例如:public Cylinder(double r, double h): base(r, h) {} 

這類似於C++的初始化列表。

  1 // cs_virtual_keyword.cs  2   3 using System;  4   5 class TestClass  6   7 {   8   9     public class Dimensions 10  11     {  12  13         public const double PI = Math.PI; 14  15         protected double x, y; 16  17         public Dimensions()         { 18  19         }  20  21         public Dimensions(double x, double y)        {  22  23             this.x = x; 24  25             this.y = y; 26  27         }  28  29         public virtual double Area()        {  30  31             return x * y; 32  33         } 34  35     } 36  39     public class Circle : Dimensions    {  40  41         public Circle(double r) : base(r, 0) 42  43         { 44  45         } 46  47   48  49         public override double Area() 50  51         {  52  53             return PI * x * x; 54  55         } 56  57     } 58  59   60  61     class Sphere : Dimensions 62  63     {  64  65         public Sphere(double r) : base(r, 0) 66  67         { 68  69         } 70  71   72  73         public override double Area() 74  75         {  76  77             return 4 * PI * x * x; 78  79         } 80  81     } 82  85     class Cylinder : Dimensions 86  87     {  88  89         public Cylinder(double r, double h) : base(r, h) 90  91         { 92  93         }  94  95         public override double Area() 96  97         {  98  99             return 2 * PI * x * x + 2 * PI * x * y;100 101         }102 103     }104 107     static void Main()108 109     { 110 111         double r = 3.0, h = 5.0; 112 113         Dimensions c = new Circle(r);114 115         Dimensions s = new Sphere(r); 116 117         Dimensions l = new Cylinder(r, h);118 119         // Display results: 120 121         Console.WriteLine("Area of Circle   = {0:F2}", c.Area()); 124 125         Console.WriteLine("Area of Sphere   = {0:F2}", s.Area()); 128 129         Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());132 133     }134 135 } 

輸出 

1 Area of Circle   = 28.27;2 3 Area of Sphere   = 113.10;4 5 Area of Cylinder = 150.80 ;

 

virtual 修飾符 C# .NET

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.