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

继承和抽象类中提到过,子类与父类的方法间有这些关系:
子类直接使用父类方法(但是必须父类方法是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;        }        ///         /// 构造器        ///         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;            }        }        ///         /// 构造器        ///         public YTeacher(int id, string name)        {            this.id = id;            this.name = name;        }        ///         /// 构造器        ///         public YTeacher(int id, string name, YSchool school)        {            this.id = id;            this.name = name;            this.school = school;        }        ///         /// 给学生讲课的方法        ///         public void ToTeachStudents()        {            Console.WriteLine(string.Format(@"{0} 老师教育同学们: Good Good Study,Day Day Up!", this.Name));        }        ///         /// 惩罚犯错误学生的方法        /// 加virtual关键字,表示该方法可以被覆盖重写        ///         ///         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)        {        }        ///         /// 隐藏父类的同名方法,隐藏后该类只能访问隐藏后的方法,不能访问到父类的该方法了。        ///         public new void ToTeachStudents()        {            Console.WriteLine(string.Format(@"{0} 老师教育同学们:认真学习.net!", this.Name));        }                ///         /// 覆盖        ///         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();        }    }}

结果:

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

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

以上就是C#基础知识整理:基础知识(7) 方法的隐藏的内容,更多相关内容请关注PHP中文网(www.php.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432214.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 06:22:13
下一篇 2025年12月17日 06:22:23

相关推荐

发表回复

登录后才能评论
关注微信