生活随笔
收集整理的這篇文章主要介紹了
C#基础知识整理:基础知识(5) 方法的重载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??? 老師都有講課這個方法,一個老師先是在西部偏遠山區,是站在教室里木頭的黑板前講課;過了幾年表現好,調到了稍微好點的城市里,是坐在教室前用多媒體設備講課;又過了幾年考博士了,畢業后繼續當老師,不過現在是躺在家里對著電腦遠程授課。都是講課這個方法,不同的條件下(參數不同)有不同的執行過程和輸出結果。這就是重載。
重載的定義是:在同一個類中 ,或者是這個類的子類中,有若干個同名的方法就是重載,不過方法同名但是參數列表必須不同。在子類的情況就是,子類有和父類方法名相同但參數列表不同的方法,而且父類的該名字的方法必須為protected和public型的。
看下面代碼:
??? 學校高考完后,有好幾個被北大和清華錄取了,于是學校請老師去五星級酒店吃飯。門迎見到顧客光臨,要稱呼:男士/女士,歡迎光臨!
using System; namespace YYS.CSharpStudy.MainConsole { public class YSchool { private int id = 0; private string name = string.Empty; public int ID { get { return this.id; } } public string Name { get { return name; } } public YSchool() { this.id = 0; this.name = @"清華大學附中"; } public YSchool(int id, string name) { this.id = id; this.name = name; } /// <summary> /// 構造器 /// </summary> public YSchool(int id) { this.id = id; this.name = @"陜師大附中"; } } public class YTeacher { private int id = 0; private string name = string.Empty; private YSchool school = null; private string introDuction = string.Empty; private string imagePath = string.Empty; public int ID { get { return id; } } public string Name { get { return name; } } public YSchool School { get { if (school == null) { school = new YSchool(); } return school; } set { school = value; } } public string IntroDuction { get { return introDuction; } set { introDuction = value; } } public string ImagePath { get { return imagePath; } set { imagePath = value; } } /// <summary> /// 構造器 /// </summary> public YTeacher(int id, string name) { this.id = id; this.name = name; } /// <summary> /// 構造器 /// </summary> public YTeacher(int id, string name, YSchool school) { this.id = id; this.name = name; this.school = school; } /// <summary> /// 給學生講課的方法 /// </summary> public void ToTeachStudents() { Console.WriteLine(string.Format(@"{0} 老師教育同學們: Good Good Study,Day Day Up!", this.name)); } /// <summary> /// 懲罰犯錯誤學生的方法 /// </summary> /// <param name="punishmentContent"></param> public void PunishmentStudents(string punishmentContent) { Console.WriteLine(string.Format(@"{0} 的{1} 老師讓犯錯誤的學生 {2}。", this.School.Name, this.name, punishmentContent)); } } public class MrTeacher : YTeacher { public MrTeacher(int id, string name) : base(id, name) { } /// <summary> /// 擴展的方法,刮胡子方法。 /// </summary> public void Shave() { Console.WriteLine(string.Format(@"{0} 老師用飛科剃須刀刮胡子。",this.Name)); } } public class MisTeacher : YTeacher { public MisTeacher(int id, string name) : base(id, name) { } /// <summary> /// 擴展方法,護膚的方法 /// </summary> public void SkinCare() { Console.WriteLine(string.Format(@"{0} 老師用香奈兒護膚霜護膚。", this.Name)); } } public class FiveStarsHotel { /// <summary> /// 重載 /// </summary> public void Welcome(MrTeacher mTeacher) { Console.WriteLine(@"先生,歡迎光臨!"); } /// <summary> /// 重載 /// </summary> public void Welcome(MisTeacher misTeacher) { Console.WriteLine(@"女士,歡迎光臨!"); } } }using System; namespace YYS.CSharpStudy.MainConsole { class Program { static void Main(string[] args) { FiveStarsHotel hotel = new FiveStarsHotel(); MrTeacher mrTeacher = new MrTeacher(1, @"牛轟轟"); Console.WriteLine(@"牛轟轟 來了"); hotel.Welcome(mrTeacher);//男老師進門 MisTeacher misTeacher = new MisTeacher(2, @"郝漂靚"); Console.WriteLine(@"郝漂靚 來了"); hotel.Welcome(misTeacher);//女老師進門 Console.ReadKey(); } } }
結果:
?
?? 看上面的代碼中,YTeacher,YSchool中的構造器就是重載的運用。
??? 重載的好處是可以讓邏輯更明確,比如上述代碼中,Welcome方法其實也可以寫一個方法,然后使用if else或者switch語句來判斷,最后輸出結果。但是我們完成一個工程不光是為了完成某個功能,還要讓代碼可讀性強,邏輯明確,易于維護,所以就要讓代碼在邏輯上更接近于現實世界的邏輯。使用重載能讓代碼更好理解,執行步驟也很直觀。
代碼下載:http://download.csdn.net/detail/yysyangyangyangshan/4393502
總結
以上是生活随笔為你收集整理的C#基础知识整理:基础知识(5) 方法的重载的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。