C#基础知识整理:基础知识(7) 方法的隐藏

来源:互联网 发布:全息生物学 知乎 编辑:程序博客网 时间:2024/05/21 18:46

继承和抽象类中提到过,子类与父类的方法间有这些关系:
子类直接使用父类方法(但是必须父类方法是public或protected类型);
子类的方法覆盖父类方法(override);
子类的方法重载父类方法(overload);
看下面这种情况:

    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>        /// 惩罚犯错误学生的方法        /// 加virtual关键字,表示该方法可以被覆盖重写        /// </summary>        /// <param name="punishmentContent"></param>        public virtual void PunishmentStudents(string punishmentContent)        {            Console.WriteLine(string.Format(@"{0} 的{1} 老师让犯错误的学生 {2}。", this.School.Name, this.name, punishmentContent));        }    }    public class UniversityTeacher : YTeacher    {        public UniversityTeacher(int id, string name,YSchool school)            : base(id, name, school)        {        }        /// <summary>        /// 隐藏父类的同名方法,隐藏后该类只能访问隐藏后的方法,不能访问到父类的该方法了。        /// </summary>        public new void ToTeachStudents()        {            Console.WriteLine(string.Format(@"{0} 老师教育同学们:认真学习.net!", this.Name));        }                /// <summary>        /// 覆盖        /// </summary>        public override void PunishmentStudents(string punishmentContent)        {            base.PunishmentStudents(punishmentContent);//也可以不执行父类方法。            //自己的代码        }    }
using System;namespace YYS.CSharpStudy.MainConsole{    class Program    {        static void Main(string[] args)        {            UniversityTeacher uTeacher = new UniversityTeacher(3, @"董边成", new YSchool(3, @"清华大学"));            //访问的是子类方法            uTeacher.ToTeachStudents();            //可以访问覆盖后的方法            uTeacher.PunishmentStudents(@"让你挂科。");            YTeacher teacher = new UniversityTeacher(3, @"董边成", new YSchool(3, @"清华大学"));            //访问不到隐藏的那个方法了            teacher.ToTeachStudents();            //可以访问覆盖后的方法            teacher.PunishmentStudents(@"跑10000米。");            Console.ReadKey();        }    }}

结果:

    子类继承父类的方法,使用new修饰一个同父类方法同名,参数列表相同的新方法的过程就叫做隐藏。也就是子类隐藏了父类的这个方法。不过隐藏与覆盖不同,隐藏的方法只能通过该方法所在的类访问,如果使用父类的变量,依然访问的是被隐藏的方法。
    从上面的代码中可以看到,覆盖和隐藏的区别。父类变量引用子类实例后,只能访问被隐藏的方法,而无法访问隐藏后的方法。但是都可以访问到覆盖后的方法。
还有一点就是如果想让这个方法被子类覆盖,那么父类该方法必须加上virtual。隐藏父类的方法new关键字也可以不加。隐藏一般使用的比较少,在一些特殊的情况下解决一些问题。

代码下载:http://download.csdn.net/detail/yysyangyangyangshan/4399465

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 windows7图标变大了怎么办 igs格式烂曲面怎么办 手机桌面文件夹打不开怎么办 苹果下载不了150怎么办 iphone6速度变慢怎么办 苹果手机微信打不开pdf怎么办 苹果手机打不开pdf怎么办 pdf文件超过了怎么办 pdf电脑删不了怎么办 联想笔记本摄像头横屏调竖屏怎么办 pdf文件打开失败怎么办 pdf复制文字乱码怎么办 电子发票乱码了怎么办 超星尔雅挂了怎么办 电脑应用双击打不开怎么办 电脑控制面板打不开怎么办 转换器无法打开文件怎么办 电脑文件无法打开怎么办 手机上jpg打不开怎么办 脸上全是黄褐斑怎么办 容易发胖的体质怎么办 感冒后一直咳嗽怎么办 感冒咳嗽怎么办小窍门 到了减肥平台期怎么办 减肥遇见平台期怎么办 脚冻伤了痒怎么办 冬天脚后跟冻了怎么办 夏天脚冻了怎么办 导航软件删了怎么办 婆婆爱打孩子怎么办 乙肝婆婆带孩子怎么办 婆婆不会教孩子怎么办 婆婆不会带孩子怎么办 公婆枪带孩子怎么办 婆婆要带孩子怎么办 婆婆太惯孩子怎么办 三个月宝宝溢奶怎么办 孩子半夜咳嗽吐怎么办 中老年人转氨酶和血指高怎么办 孩子逆反心强怎么办 孩子学习逆反了怎么办