First recognized inheritance and polymorphism, first recognized inheritance Polymorphism
What is inheritance?
Inheritance uses existing class definitions as the basis to create new class technologies. New class definitions can add new data or functions, or use existing class functions.
Inheritance is a very important feature in object-oriented programming. In the two classes with inheritance relationships, the subclass not only has its own independent members, but also has members of the parent class.
The inheritance relationship represents an arrow in the class diagram, and the arrow points to the parent class.
Base keyword and protected Modifier
All know that this indicates the current instance. It can be used to access the members of the parent class. in C #, there is a keyword base, which indicates the parent class and can be used to access the members of the parent class.
In the subclass, you can use base to call the attributes of the parent class. In fact, you can also use the base keyword to call the methods of the parent class and the constructor of the parent class.
If a member in the parent class is modified using private, the member will act as a private member and cannot be accessed by any other class. If it is set to a public member, any class can access the member.
In C #, another access modifier protected is provided. The access modifier member is allowed to be accessed by its subclass, but not by other non-subclass access.
Differences between public, private, and protected
Modifier class, internal subclass, other classes
Public: Yes.
Can protected be?
Public or not.
It can be seen that the three access modifiers restrict access to class members: private> protected> public
Subclass Constructor
Call the parent class constructor implicitly.
Is a application
Inherited Value
Appreciate the inheritance features
Inheriting the relationship that simulates the real world, oop emphasizes that everything is an object, which conforms to our thinking direction of object-oriented programming.
Inheritance achieves code reuse and reasonable use of inheritance, making the code more concise
Inheritance makes the program structure clear, and the Child class and parent class have a clear hierarchy. The ultimate goal is to make the child class focus only on the behavior and Status of the Child class, and do not need to focus on the behavior and status of the parent class.
Solve the Problems Caused by inheritance
Public virtual string SayHi (){
// Method body
}
Such methods that use virtual keywords are called virtual methods. The virtual method has a method body.
Syntax
Access modifier virtual return class method name (){
// Method body
}
Public override string SayHi (){
// Method body
}
A method such as the override keyword is called method rewriting. Virtual methods can be overwritten.
Syntax
Access modifier override
Return class method name (){
// Method body
}
What is polymorphism
Implementation of Polymorphism
Override of implementation methods
Define the SayHi () method in the parent class and use the virtual keyword to define it as a virtual method.
Define the sub-class's own SayHi () in the sub-class and use the override keyword to override the SayHi () method of the parent class.
Defines parent variables and initializes parent variables with subclass objects.
Polymorphism literally means multiple forms. It means that different interpretations can be made for different objects to produce different execution results.
Polymorphism can be achieved using virtual methods.