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

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊

          實用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114333
          • 排名 - 515

          最新評論

          閱讀排行榜

          評論排行榜

          覆蓋類成員:通過new關(guān)鍵字修飾虛函數(shù)表示覆蓋該虛函數(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)
                  {
                      
          //定義兩個對象,一個父類對象,一個子類對象
                      Person aPerson = new ChinesePerson();
                      ChinesePerson cnPerson 
          = new ChinesePerson();
                      
          //調(diào)用覆蓋的方法,父類對象不能調(diào)用子類覆蓋過的方法,只能調(diào)用自身的虛函數(shù)方法
                      aPerson.IntroduceMyself();      
                      cnPerson.IntroduceMyself();
                      
          //調(diào)用重載方法,父類對象和子類對象都可以調(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 期待明天 閱讀(750) 評論(0)  編輯  收藏 所屬分類: CSharp
          主站蜘蛛池模板: 安塞县| 甘洛县| 吉隆县| 高阳县| 大同县| 石棉县| 上犹县| 江城| 河源市| 镇赉县| 宁阳县| 西青区| 江达县| 唐海县| 洛阳市| 兰考县| 桃江县| 夏河县| 玛曲县| 宁强县| 景谷| 垫江县| 驻马店市| 苍梧县| 西乡县| 凤阳县| 天台县| 大连市| 彰化县| 绵阳市| 门头沟区| 门源| 保亭| 保靖县| 新巴尔虎右旗| 巨野县| 丽江市| 新建县| 庆阳市| 教育| 沾化县|