您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/8/2 21:48:09发布14次查看
继承和抽象类中提到过,子类与父类的方法间有这些关系:
子类直接使用父类方法(但是必须父类方法是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关键字也可以不加。隐藏一般使用的比较少,在一些特殊的情况下解决一些问题。
以上就是c#基础知识整理:基础知识(7) 方法的隐藏的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product