隨風設計

          隨風設計

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks

          本教程使用 overridenew 關鍵字來演示 C# 中的版本控制。版本控制在基類和派生類衍生時維護它們之間的兼容性。

          教程

          C# 語言被設計為不同庫中的基類和派生類之間的版本控制可以衍生,并保持向后兼容。例如,這意味著在基類中引入與派生類中的某個成員名稱相同的新成員不是錯誤。它還意味著類必須顯式聲明某方法是要重寫一個繼承方法,還是一個僅隱藏具有類似名稱的繼承方法的新方法。

          在 C# 中,默認情況下方法不是虛擬的。若要使方法成為虛擬方法,必須在基類的方法聲明中使用 virtual 修飾符。然后,派生類可以使用 override 關鍵字重寫基虛擬方法,或使用 new 關鍵字隱藏基類中的虛擬方法。如果 override 關鍵字和 new 關鍵字均未指定,編譯器將發出警告,并且派生類中的方法將隱藏基類中的方法。下面的示例在實際操作中展示這些概念。

          示例

          // versioning.cs
          // CS0114 expected
          public class MyBase 
          {
             public virtual string Meth1() 
             {
                return "MyBase-Meth1";
             }
             public virtual string Meth2() 
             {
                return "MyBase-Meth2";
             }
             public virtual string Meth3() 
             {
                return "MyBase-Meth3";
             }
          }
          
          class MyDerived : MyBase 
          {
             // Overrides the virtual method Meth1 using the override keyword:
             public override string Meth1() 
             {
                return "MyDerived-Meth1";
             }
             // Explicitly hide the virtual method Meth2 using the new
             // keyword:
             public new string Meth2() 
             {
                return "MyDerived-Meth2";
             }
             // Because no keyword is specified in the following declaration
             // a warning will be issued to alert the programmer that 
             // the method hides the inherited member MyBase.Meth3():
             public string Meth3() 
             {
                return "MyDerived-Meth3";
             }
          
             public static void Main() 
             {
                MyDerived mD = new MyDerived();
                MyBase mB = (MyBase) mD;
          
                System.Console.WriteLine(mB.Meth1());
                System.Console.WriteLine(mB.Meth2());
                System.Console.WriteLine(mB.Meth3());
             }
          }

          輸出

          MyDerived-Meth1
          MyBase-Meth2
          MyBase-Meth3

          代碼討論

          從派生類隱藏基類成員在 C# 中不是錯誤。該功能使您可以在基類中進行更改,而不會破壞繼承該基類的其他庫。例如,某個時候可能有以下類:

          class Base {}
          class Derived: Base
          {
             public void F() {}
          }

          稍后基類可能演變為添加了一個 void 方法 F(),如下所示:

          class Base 
          {
             public void F() {}
          }
          class Derived: Base
          {
             public void F() {}
          }

          因此,在 C# 中,基類和派生類都可以自由演變,并能夠維持二進制兼容性。

          posted on 2006-08-17 11:35 曹賢 閱讀(178) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 余干县| 称多县| 苍山县| 嘉峪关市| 高邮市| 泗水县| 红桥区| 那曲县| 孙吴县| 浏阳市| 额尔古纳市| 铜山县| 报价| 通渭县| 巩留县| 古浪县| 自贡市| 恩施市| 桃源县| 安远县| 盐山县| 桓仁| 扶沟县| 凤阳县| 保山市| 德兴市| 五家渠市| 佛山市| 大厂| 福州市| 灵寿县| 嘉兴市| 方山县| 灵川县| 正宁县| 芷江| 綦江县| 贵州省| 延边| 文安县| 西青区|