Sixth Chapter Succession
1. Understanding the concept of inheritance
2. Proficient in using inheritance to establish parent classes and subclasses
3. Understanding the concept of polymorphism
4. The virtual method is overridden to implement polymorphism
1. Inheritance:
In C #, if a class is followed by a colon and followed by another class, then we call the class before the colon
is a subclass, and the class after the colon is the parent class. The relationship that this notation shows is called the inheritance relationship of the class.
Subclass: Derived class
Parent class: base class or Super class
Calling the parent class constructor from base notice point
01. Calling the parent class constructor through base can only be written after the construction of the subclass
02. Calling the parent class through base constructor parameter order is consistent with the parent class's construction parameters
2. Access modifiers
Public: can be accessed anywhere, even across assemblies.
Private: can only be accessed in curly brackets of the current class.
Protected: can only be accessed in the current class, subclasses of the current class, subclasses of subclasses (grandchildren Class).
3. Transitive nature of inheritance
If a Class B inherits from Class A, and Class C inherits Class B, then Class C can also access the non-private members of Class A
Inheritance of the single root (C # does not support multi-inheritance Java and multi-inheritance is not supported)
In C #, a class can have only one parent class.
4. polymorphic
1. Different objects respond differently to the same operation, which is known as polymorphic in object-oriented programming
2. Using polymorphism is for unified invocation
3. Achieve polymorphism in two ways:
Mode one: Using virtual method to realize polymorphism
Steps to achieve polymorphism
01. Define a virtual method with the virtual keyword in the parent class
02. Overriding a virtual method in a parent class with the override keyword in a subclass
The virtual method SayHello () is defined by using the virtual keyword in the normal class person, and then overridden by the override keyword in the subclass student for the parent class's SayHello () method.
Depth. NET platform and C # programming note Chapter Sixth inheritance