隨筆 - 78  文章 - 25  trackbacks - 0
          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊(cè)

          實(shí)用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114374
          • 排名 - 515

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          覆蓋類成員:通過new關(guān)鍵字修飾虛函數(shù)表示覆蓋該虛函數(shù)。
          一個(gè)虛函數(shù)被覆蓋后,任何父類變量都不能訪問該虛函數(shù)的具體實(shí)現(xiàn)。
          public virtual void IntroduceMyself(){...}//父類虛函數(shù)
          public new void IntroduceMyself(){...}//子類覆蓋父類虛函數(shù)

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace MethodOverrideByNew
          {
              
          public enum Genders { 
                  Female
          =0,
                  Male
          =1
              }
              
          public class Person {
                  
          protected string _name;
                  
          protected int _age;
                  
          protected Genders _gender;
                  
          /// <summary>
                  /// 父類構(gòu)造函數(shù)
                  /// </summary>
                  public Person() {
                      
          this._name = "DefaultName";
                      
          this._age = 23;
                      
          this._gender = Genders.Male;
                  }
                  
          /// <summary>
                  /// 定義虛函數(shù)IntroduceMyself()
                  /// </summary>
                  public virtual void IntroduceMyself() {
                      System.Console.WriteLine(
          "Person.IntroduceMyself()");
                  }
                  
          /// <summary>
                  /// 定義虛函數(shù)PrintName()
                  /// </summary>
                  public virtual void PrintName() {
                      System.Console.WriteLine(
          "Person.PrintName()");
                  }
              }
              
          public class ChinesePerson :Person{
                  
          /// <summary>
                  /// 子類構(gòu)造函數(shù),指明從父類無參構(gòu)造函數(shù)調(diào)用起
                  /// </summary>
                  public ChinesePerson() :base(){
                      
          this._name = "DefaultChineseName";
                  }
                  
          /// <summary>
                  /// 覆蓋父類方法IntroduceMyself,使用new關(guān)鍵字修飾虛函數(shù)
                  /// </summary>
                  public new void IntroduceMyself() {
                      System.Console.WriteLine(
          "ChinesePerson.IntroduceMyself()");
                  }
                  
          /// <summary>
                  /// 重載父類方法PrintName,使用override關(guān)鍵字修飾虛函數(shù)
                  /// </summary>
                  public override void PrintName(){
                      System.Console.WriteLine(
          "ChinesePerson.PrintName()");            
                  }
              }

              
          class Program
              {
                  
          static void Main(string[] args)
                  {
                      
          //定義兩個(gè)對(duì)象,一個(gè)父類對(duì)象,一個(gè)子類對(duì)象
                      Person aPerson = new ChinesePerson();
                      ChinesePerson cnPerson 
          = new ChinesePerson();
                      
          //調(diào)用覆蓋的方法,父類對(duì)象不能調(diào)用子類覆蓋過的方法,只能調(diào)用自身的虛函數(shù)方法
                      aPerson.IntroduceMyself();      
                      cnPerson.IntroduceMyself();
                      
          //調(diào)用重載方法,父類對(duì)象和子類對(duì)象都可以調(diào)用子類重載過后的方法
                      aPerson.PrintName();
                      cnPerson.PrintName();

                      System.Console.ReadLine();
                  }
              }
          }

          結(jié)果:
          Person.IntroduceMyself()
          ChinesePerson.IntroduceMyself()
          ChinesePerson.PrintName()
          ChinesePerson.PrintName()



          posted on 2009-10-26 15:57 期待明天 閱讀(751) 評(píng)論(0)  編輯  收藏 所屬分類: CSharp
          主站蜘蛛池模板: 三明市| 郴州市| 乐平市| 从化市| 孝昌县| 静安区| 平泉县| 安塞县| 嫩江县| 米易县| 白水县| 新平| 兴城市| 胶州市| 遂川县| 乌海市| 睢宁县| 塘沽区| 清流县| 屏东市| 波密县| 古丈县| 广宁县| 南雄市| 额济纳旗| 邢台县| 湾仔区| 华坪县| 沛县| 昂仁县| 新田县| 泰顺县| 凉山| 怀集县| 华池县| 睢宁县| 泰宁县| 郑州市| 尖扎县| 微博| 五华县|