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

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊

          實(shí)用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114325
          • 排名 - 515

          最新評論

          閱讀排行榜

          評論排行榜

          方法重載,在C#中通過虛函數(shù)來實(shí)現(xiàn),具體做法:現(xiàn)在父類中用virtual將其聲明為虛函數(shù),然后在子類中用override關(guān)鍵字來指定該函數(shù)為重載函數(shù)。重載函數(shù)必須具有父類函數(shù)中的參數(shù)個數(shù),參數(shù)類型和返回類型。

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

          namespace MethodOverride
          {
              
          public enum Genders { 
                  Female
          =0,
                  Male
          =1
              }
              
          public class Person {
                  
          protected string _name;
                  
          protected int _age;
                  
          protected Genders _gender;
                  
          public Person() {
                      
          this._name = "default name";
                      
          this._age = 20;
                      
          this._gender = Genders.Male;
                  }
                  
          public virtual void IntroduceMyself() {
                      System.Console.WriteLine(
          "introduce myself");
                  }
              }

              
          public class ChinesePerson : Person {
                  
          public ChinesePerson() : base() {
                      
          this._name = "default Chinese Name";            
                  }
                  
          public override void IntroduceMyself()
                  {
                      System.Console.WriteLine(
          "我叫{0},年齡{1},性別{2}",this._name,this._age,this._gender);
                  }
              }

              
          public class EnglishPerson : Person
              {
                  
          public EnglishPerson(): base()
                  {
                      
          this._name = "default English Name";
                  }
                  
          public override void IntroduceMyself()
                  {
                      System.Console.WriteLine(
          "My Name is{0},my age is {1},my gender is {2}"this._name, this._age, this._gender);
                  }
              }

              
          class Program
              {
                  
          static void Main(string[] args)
                  {
                      Person aPerson 
          = new Person();
                      aPerson.IntroduceMyself();
                      aPerson 
          = new ChinesePerson();
                      aPerson.IntroduceMyself();
                      aPerson 
          = new EnglishPerson();
                      aPerson.IntroduceMyself();
                      System.Console.ReadLine();
                  }
              }
          }

          結(jié)果如下:
          introduce myself
          我叫default Chinese Name,年齡20,性別Male
          My Name isdefault English Name,my age is 20,my gender is Male



          posted on 2009-10-26 14:07 期待明天 閱讀(988) 評論(0)  編輯  收藏 所屬分類: CSharp
          主站蜘蛛池模板: 兴安县| 西盟| 仁寿县| 昆明市| 仪陇县| 宿州市| 哈密市| 桦甸市| 伊宁县| 佳木斯市| 陵水| 霍林郭勒市| 同仁县| 临西县| 上犹县| 江西省| 唐河县| 郴州市| 驻马店市| 河间市| 商洛市| 安乡县| 抚远县| 和顺县| 塔城市| 米易县| 界首市| 榆中县| 微山县| 大兴区| 漳平市| 高雄县| 栖霞市| 南木林县| 丰城市| 仙桃市| 和政县| 长治县| 麻城市| 乡城县| 泾阳县|