As mentioned earlier, the three major characteristics of object-oriented: encapsulation, inheritance and polymorphism. We have almost fully understood the encapsulation in the definition of the previous class. Now let’s look at the characteristics of inheritance.
Inheritance is actually an extension of one class to another class. The latter is called a base class, and the former is called a subclass. Inheritance means that the subclass has all the attributes and methods of the base class, and the subclass can also add attributes and methods. However, subclasses cannot remove the attributes and methods of the parent class.
Of course, the issue of modifiers is also mentioned here. The subclass has all the properties and methods of the base class, but it does not mean that the subclass can arbitrarily access these inherited properties and methods. Subclasses can only access public and protected properties and methods, and the rest cannot be accessed directly. Another thing is that static properties and methods cannot be inherited, because the static type is related to the class and has nothing to do with the object.
Look at the code:
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; } } /// <summary> /// 构造器 /// </summary> public YSchool() { this.id = 0; this.name = @"清华大学附中"; } /// <summary> /// 构造器 /// </summary> 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; /// <summary> /// 使用只读属性,因为是固有属性。 /// </summary> public int ID { get { return id; } } public string Name { get { return name; } } /// <summary> /// 这几个使用get/set属性,因为这几个属性不是固有的,可以随着某些条件改变的。 /// </summary> 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; } /// <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)); } } /// <summary> /// 男性老师,继承自YTeacher /// </summary> public class MrTeacher : YTeacher { /// <summary> /// 构造器,这里要注意,YTeacher是没有提供默认构造器的, /// 所以子类必须要有构造器,并且和基类参数列表一致。 /// </summary> /// <param name="id"></param> /// <param name="name"></param> public MrTeacher(int id, string name) : base(id, name) { } /// <summary> /// 扩展的方法,刮胡子方法。 /// </summary> public void Shave() { Console.WriteLine(string.Format(@"{0} 老师用飞科剃须刀刮胡子。",this.Name)); } } /// <summary> /// 女性老师,继承自YTeacher /// </summary> public class MisTeacher : YTeacher { /// <summary> /// 构造函数 /// </summary> /// <param name="id"></param> /// <param name="name"></param> public MisTeacher(int id, string name) : base(id, name) { } /// <summary> /// 扩展方法,护肤的方法 /// </summary> public void SkinCare() { Console.WriteLine(string.Format(@"{0} 老师用香奈儿护肤霜护肤。",this.Name)); } } }
using System; namespace YYS.CSharpStudy.MainConsole { class Program { static void Main(string[] args) { MrTeacher mrTeacher = new MrTeacher(1, @"牛轰轰"); mrTeacher.ToTeachStudents(); mrTeacher.PunishmentStudents(@"背唐诗"); mrTeacher.Shave(); MisTeacher misTeacher = new MisTeacher(2, @"郝漂靓"); misTeacher.ToTeachStudents(); misTeacher.PunishmentStudents(@"默写红楼梦"); misTeacher.SkinCare(); Console.ReadKey(); } }
Result:
Inheritance is an object-oriented feature, and its benefits are:
First, when doing project design classes , inheritance allows us to omit a lot of code;
Second, it conforms to the organizational form of classes in object-oriented. In advanced object-oriented languages such as C# and Java, there is an object class, which is the ancestor class of all classes. All classes are derived from him.
One thing you need to pay attention to when inheriting is the constructor of subclasses. During the program running process, the subclass first calls the constructor of the parent class. If the subclass does not write a constructor, the default constructor of the parent class will be called. If the parent class does not have a default constructor, that is, the parent class writes a constructor with parameters, then the subclass will also call the constructor with parameters, but you need to specify which constructor with parameters is called (see the base in the code ).
The above is the summary of C# basic knowledge: basic knowledge (4) inheritance content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!