老師都有講課這個方法,一個老師先是在西部偏遠山區,是站在教室裡木頭的黑板前講課;過了幾年表現好,調到了稍微好點的城市裡,是坐在教室前用多媒體設備講課;又過了幾年考博士了,畢業後繼續當老師,不過現在是躺在家裡對著電腦遠距授課。都是講課這個方法,不同的條件(參數不同)有不同的執行過程和輸出結果。這就是重載。
重載的定義是:在同一個類別中 ,或者是這個類別的子類別中,有若干個同名的方法就是重載,不過方法同名但是參數列表必須不同。在子類別的情況就是,子類別有和父類別方法名稱相同但參數清單不同的方法,而且父類別的該名字的方法必須為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語句來判斷,最後輸出結果。但是我們完成一個工程不光是為了完成某個功能,還要讓程式碼可讀性強,邏輯明確,易於維護,所以就要讓程式碼在邏輯上更接近現實世界的邏輯。使用重載能讓程式碼更好理解,執行步驟也很直覺。
以上就是C#基礎知識整理:基礎知識(5) 方法的重載的內容,更多相關內容請關注PHP中文網(m.sbmmt.com)!