隨風(fēng)設(shè)計(jì)

          隨風(fēng)設(shè)計(jì)

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 0 評(píng)論 :: 0 Trackbacks

          本教程使用 overridenew 關(guān)鍵字來(lái)演示 C# 中的版本控制。版本控制在基類(lèi)和派生類(lèi)衍生時(shí)維護(hù)它們之間的兼容性。

          教程

          C# 語(yǔ)言被設(shè)計(jì)為不同庫(kù)中的基類(lèi)和派生類(lèi)之間的版本控制可以衍生,并保持向后兼容。例如,這意味著在基類(lèi)中引入與派生類(lèi)中的某個(gè)成員名稱(chēng)相同的新成員不是錯(cuò)誤。它還意味著類(lèi)必須顯式聲明某方法是要重寫(xiě)一個(gè)繼承方法,還是一個(gè)僅隱藏具有類(lèi)似名稱(chēng)的繼承方法的新方法。

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

          示例

          // 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

          代碼討論

          從派生類(lèi)隱藏基類(lèi)成員在 C# 中不是錯(cuò)誤。該功能使您可以在基類(lèi)中進(jìn)行更改,而不會(huì)破壞繼承該基類(lèi)的其他庫(kù)。例如,某個(gè)時(shí)候可能有以下類(lèi):

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

          稍后基類(lèi)可能演變?yōu)樘砑恿艘粋€(gè) void 方法 F(),如下所示:

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

          因此,在 C# 中,基類(lèi)和派生類(lèi)都可以自由演變,并能夠維持二進(jìn)制兼容性。

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

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 北票市| 梧州市| 喀什市| 高台县| 张家口市| 莎车县| 洛宁县| 鸡泽县| 浙江省| 蕲春县| 库车县| 长治县| 扶绥县| 柳江县| 江陵县| 梓潼县| 台山市| 齐河县| 济宁市| 宁明县| 炎陵县| 宁远县| 富锦市| 盈江县| 宜丰县| 麦盖提县| 海城市| 咸阳市| 抚州市| 甘孜| 武穴市| 马关县| 嘉荫县| 安阳市| 乐东| 兴隆县| 将乐县| 澄迈县| 剑阁县| 青川县| 嘉黎县|