程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#基礎知識整理:基礎知識(5) 方法的重載

C#基礎知識整理:基礎知識(5) 方法的重載

編輯:C#入門知識

 老師都有講課這個方法,一個老師先是在西部偏遠山區,是站在教室裡木頭的黑板前講課;過了幾年表現好,調到了稍微好點的城市裡,是坐在教室前用多媒體設備講課;又過了幾年考博士了,畢業後繼續當老師,不過現在是躺在家裡對著電腦遠程授課。都是講課這個方法,不同的條件下(參數不同)有不同的執行過程和輸出結果。這就是重載。
重載的定義是:在同一個類中 ,或者是這個類的子類中,有若干個同名的方法就是重載,不過方法同名但是參數列表必須不同。在子類的情況就是,子類有和父類方法名相同但參數列表不同的方法,而且父類的該名字的方法必須為protected和public型的。
看下面代碼:
    學校高考完後,有好幾個被北大和清華錄取了,於是學校請老師去五星級酒店吃飯。門迎見到顧客光臨,要稱呼:男士/女士,歡迎光臨!

[csharp] 
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
{
    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(@"女士,歡迎光臨!");
        }
    }
}[csharp] 
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(); 
        } 
    } 

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();
        }
    }
}
結果:
\
作者:yysyangyangyangshan
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved