C # object-oriented -- Inheritance,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.