C#基础知识整理:基础知识(5) 方法的重载

黄舟
黄舟 原创
2017-02-11 13:13:26 801浏览

老师都有讲课这个方法,一个老师先是在西部偏远山区,是站在教室里木头的黑板前讲课;过了几年表现好,调到了稍微好点的城市里,是坐在教室前用多媒体设备讲课;又过了几年考博士了,毕业后继续当老师,不过现在是躺在家里对着电脑远程授课。都是讲课这个方法,不同的条件下(参数不同)有不同的执行过程和输出结果。这就是重载。
重载的定义是:在同一个类中 ,或者是这个类的子类中,有若干个同名的方法就是重载,不过方法同名但是参数列表必须不同。在子类的情况就是,子类有和父类方法名相同但参数列表不同的方法,而且父类的该名字的方法必须为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)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。