As mentioned above, three main characteristics of object-oriented are encapsulation, inheritance and polymorphism. The definition of the class in the front is almost thorough. Now let's look at the inherited features.
Inheritance is actually an extension of a 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 attributes and methods of the base class, And the subclass can also add attributes and methods. However, the attributes and methods of the parent class cannot be removed from subclass.
Of course, we also need to mention the modifier. The subclass has all the attributes and methods of the base class, which does not mean that the subclass can access the inherited attributes and methods at will. Sub-classes can only access public and protected attributes and methods, while others cannot be accessed directly. Another is that static attributes and methods cannot be inherited because the sum of static types has a relationship with objects.
Check 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> /// constructor /// </Summary> Public yschool () {This. id = 0; this. name = @ "";} // <summary> // constructor // </Summary> Public yschool (int id, string name) {This. id = ID; t His. name = Name;} // <summary> // constructor // </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> // use the read-only attribute because it is an inherent attribute. /// </Summary> Public int ID {get {return ID;} public string name {get {return name ;}} /// <summary> /// the get/set attributes are used, because these attributes are not inherent and can be changed with certain conditions. /// </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> // learn Method for giving lectures /// </Summary> Public void toteachstudents () {console. writeline (string. format (@ "{0} instructor Education Students: Good good study, day up! ", This. name ));} /// <summary> // Method for punishing students who make mistakes /// </Summary> /// <Param name = "punishmentcontent"> </param> Public void punishmentstudents (string punishmentcontent) {console. writeline (string. format (@ "{0}'s {1} teacher asked students who made mistakes {2 }. ", This. school. name, this. name, punishmentcontent) ;}/// <summary> // male instructor, inherited from yteacher /// </Summary> public class mrteacher: yteacher {// <summary> // constructor. Note that the default constructor is not provided for yteacher, and the sub-class must have a constructor, and the base-class parameter list is consistent. /// </Summary> /// <Param name = "ID"> </param> /// <Param name = "name"> </param> Public mrteacher (int ID, string name): Base (ID, name) {}/// <summary> /// extended method, shaving method. /// </Summary> Public void shave () {console. writeline (string. Format (@ "{0}) the instructor shave his beard with the Feike razor. ", This. name); }}/// <summary> // female teacher, inherited from yteacher // </Summary> public class misteacher: yteacher {// <summary> /// constructor // </Summary> /// <Param name = "ID"> </param> // <Param name = "name"> </param> Public misteacher (int id, string name): Base (ID, name) {}/// <summary> // extension method, skin care method /// </Summary> Public void skincare () {console. writeline (string. format (@ "{0} the instructor uses Chanel skin cream for skin care. ", This. Name ));}}}
Using system; namespace yys. csharpstudy. mainconsole {class program {static void main (string [] ARGs) {mrteacher = new mrteacher (1, @ ""); mrteacher. toteachstudents (); mrteacher. punishmentstudents (@ "Back Tang Poetry"); mrteacher. shave (); misteacher = new misteacher (2, @ "haobaojing"); misteacher. toteachstudents (); misteacher. punishmentstudents (@ "Write A Dream of Red Mansions"); misteacher. skincare (); console. readkey ();}}
Result:
Inheritance is an object-oriented feature, and its advantages are:
1. Inheritance allows us to omit the amount of code when designing a project;
Second, it conforms to the organizational form of object-oriented classes. In C #, Java, and other advanced object-oriented languages, there is an object class, which is the ancestor class of all classes, other classes are derived from them.
One thing to note about inheritance is the sub-class constructor. The program running process is a subclass that first calls the constructor of the parent class. If the child class does not have a write constructor, the default constructor of the parent class is called. If the parent class does not have a default constructor, that is, if the parent class writes a constructor with parameters, the subclass also calls the constructor with parameters, however, you need to specify which parameter constructor is called (see the base in the Code ).
Code download: http://download.csdn.net/detail/yysyangyangyangshan/4391740