子类直接运用父类要领(然则必需父类要领是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) 要领的隐蔽的内容,更多相关内容请关注ki4网(www.ki4.cn)!