inherit
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define another class according to one class, which makes it easier to create and maintain an application, and is also conducive to code reuse and development time saving.
Syntax:
[access modifier] Class class Class name 1 (base Class or parent Class)
{
···
}
[access modifier] Class class Class name 2 (derived Class or subclass): Class name 1
{
···
}
object is the base class of all classes.
//Suppose there is a base class, Person, whose derived class is Student //Base class public Class Person { private string _name; private int _age; private char _gender; } //Derived class publuc Class Student:Person { private int _id; }
What does a derived class inherit from the base class?
The derived class inherits the properties and methods of the base class, but does not inherit the private fields of the base class.
Does a derived class have a constructor that inherits the base class?
The derived class does not have a constructor that inherits the base class. However, the derived class will call the parameterless constructor of the base class by default to create the base class object so that the derived class can use the members of the base class. Therefore, if you rewrite a constructor with parameters in the base class, the one without parameters will be killed, and the derived class will not be called, so the derived class will report an error.
resolvent:
1) , rewrite a parameterless constructor in the parent class.
2) , constructor of calling parent class displayed in the child class, using the keyword * *: base()**
Inherited properties
1. Inheritance singleness: refers to that a subclass can only have one parent class
2. Transitivity of inheritance:
//My name is zhang. I'm a basketball lover. I like playing basketball. I'm 18 years old. I'm a boy class Program { static void Main(string[] args) { Reporter re = new Reporter("zhang",18,'male',"Play basketball"); re.ReporterSayHello(); Console.ReadKey(); } } public class Person//Base class { private string _name; private int _age; private char _gender; public string Name { get { return _name; } set { _name = value; } } public int Age { get { return _age; } set { _age = value; } } public char Gender { get { return _gender; } set { _gender = value; } } public Person(string name, int age, char gender) { this.Name = name; this.Age = age; this.Gender = gender; } } public class Reporter : Person//Derived class { private string _hobby; public string Hobby { get { return _hobby; } set { _hobby = value; } } public Reporter(string name, int age, char gender, string hobby) : base(name, age, gender) { this.Hobby = hobby; } public void ReporterSayHello() { Console.WriteLine("My name is{0},I am a basketball lover. My hobby is{1},I am{2}This year, I{3}Age",Name,Hobby,Gender,Age); } }
new keyword
Function of new keyword:
1. Create objects
2. Hide the member with the same name inherited from the base class (hidden with the same name). The hidden consequence is that the derived class cannot call the member of the base class
class Program { static void Main(string[] args) { Reporter rep = new Reporter(); rep.SayHello(); rep.Text(); Console.ReadKey(); } } public class Person { private string _name; private int _age; private char _gender; public string Name { get { return _name; } set { _name = value; } } public int Age { get { return _age; } set { _age = value; } } public char Gender { get { return _gender; } set { _gender = value; } } public Person() { } public void SayHello() { Console.WriteLine("Hello, I'm human"); } public void Text() { Console.WriteLine("test"); } } public class Reporter : Person { private string _hobby; public string Hobby { get { return _hobby; } set { _hobby = value; } } public Reporter() { } public new void SayHello() { Console.WriteLine("Hello, I'm a reporter"); } }