C # object-oriented -- Inheritance,
Inheritance: A combination of code and text
Class Program {static void Main (string [] args) {// Student s = new Student (); // Driver d = new Driver (); student s = new Student ("Student", 18, 'mal', 101 );}} /// <summary> // parent class -- human // </summary> public class Person {private string _ name; public string Name {get {return _ name ;} set {_ name = value ;}} private int _ age; public int Age {get {return _ age ;}set {_ age = value ;}} private char _ gender; public char Gender {get {return _ gender;} set {_ gender = value ;}} public void CHLSS () {Console. writeLine ("Eat, drink, and sleep");} public Person (string name, int age, char gender) {this. name = name; this. age = age; this. gender = gender;} public Person () {}/// <summary> // subclass -- Student /// </summary> public class Student: person {public Student (string name, int age, char gender, int id): base (name, age, gender) {// this. name = name; // this. age = age; // this. gender = gender; this. id = id;} private int _ id; public int Id {get {return _ id;} set {_ id = value;} public void Study () {Console. writeLine ("Student Union learning") ;}/// <summary> /// subclass -- instructor /// </summary> public class Teacher: person {public Teacher (string name, int age, char gender, double salary): base (name, age, gender) {this. salary = salary;} private double _ salary; public double Salary {get {return _ salary;} set {_ salary = value ;}} public void Teach () {Console. writeLine ("instructor lectures") ;}/// <summary> /// subclass -- Driver /// </summary> public class Driver: person {public Driver (string name, int age, char gender, int driveTime): base (name, age, gender) {this. dirveTime = driveTime;} private int _ dirveTime; public int DirveTime {get {return _ dirveTime;} set {_ dirveTime = value;} public void Drive () {Console. writeLine ("the driver will drive ");}}
We may write duplicate members in some classes. We can put these duplicate members,
Encapsulate them into a class as the parent class of these classes.
Student, Teacher, Driver subclass, or derived class
Person parent class or base class
Q: If the subclass inherits the parent class, what does the subclass inherit from the parent class?
First, the subclass inherits the attributes and methods of the parent class, but the subclass does not inherit the private fields of the parent class.
Q: Does the subclass inherit the constructor of the parent class?
A: The subclass does not inherit the constructor of the parent class,. Sub-classes call constructors without parameters in the parent class by default,
Creates a parent class object. Child classes can use members in the parent class.
Therefore, if a new constructor with parameters is rewritten in the parent class, the parameter-free constructor will be killed,
The subclass cannot be called, so the subclass reports an error.
Solution:
1) re-write a non-parameter constructor in the parent class.
2) Call the constructor of the parent class displayed in the subclass, using the Keyword: base ()
Note: subclass objects can call Members in the parent class, but the parent class objects can always call only their own members.
Lishi Conversion
1) subclass can be assigned to the parent class.
2) If the parent class contains a subclass object, the parent class can be converted into a subclass object.
Is: indicates the type conversion. If the conversion is successful, a true value is returned; otherwise, a false value is returned.
As: Indicates type conversion. If yes, the corresponding object is returned; otherwise, a null value is returned.