C # inheritance and polymorphism,
Inheritance: in A program, if A Class A: Class B, this mechanism is inheritance.
Can subclass inherit all content (members) of the parent class?
Resolution:
1. Private Members (attributes and methods)
2. Constructor
3. The final-modified method. The subclass cannot be rewritten.
// SE is a subclass of PM
// SE inherits PM
class SE:PM { public int n; public SE(int i,int n,int m){ } }
class PM { public int money { get; set; } public int gaga { get; set; } public PM(int money, int gaga) { this.money = money; this.gaga = gaga; } public PM() { } }
3. Access Modifier
Public
Private
Protected
In java, if you use the protected access modifier to modify a variable, all classes in the current package can be accessed. Add different packages
Classes with inheritance relationships can also be accessed.
4. Secret subclass Construction
1. Main Function
2. Subclass construction, not entering the subclass construction body
3. Execute to the parent class constructor and do not enter the parent class constructor.
4. Execute the parent class constructor to create the parent class Object
5. Return to the subclass constructor for execution.
6. The subclass constructor is executed to construct the subclass object.
7. Return to Main and continue subsequent code execution.
5. base (parent class Construction)
First usage of base: use attribute base. attribute name
Second usage of base; Use method base. The method name is the same
Third usage of base: Call the parent class to construct base ()
// Base. attribute name
class SE:PM { public int n; public SE(int i,int n,int m){ base.gaga = i; } }
// Base. Method Name
base.show();
// Call the parent class construction
public SE(int i,int n,int m):base(){ base.gaga = i; base.show(); }
6. Inheritance and uniqueness
C # does not support multi-Inheritance
Java does not support multi-inheritance. You can use interfaces to support multi-inheritance in disguise.
Multi-implementation //////////////// disguised multi-inheritance Interface
Machine language
Underlying ++ driver level of Assembly Language
Process-driven level ++ drive disc dll
C # Object-Oriented Java does not support multi-Inheritance
7. Preliminary Polymorphism
Polymorphism: Multiple Forms
Different objects have different responses to the same operation. Polymorphism.
For example:
1. USB parent USB mouse/USB keyboard/USB Lighting
2. Duck real duck/Rubber Duck wood duck (not called)
3. CUT doctors/hairdressers/actors
4. greeting in english/Korean
5. Transportation Vehicles/subway/bicycles
The parent class has a Cut method, and the subclass has a Cut method with the same name.
How to satisfy polymorphism Conditions
Parent class: Person
Subclass A: Hairdresser barber
Subclass B: Doctor
Subclass C: Actor
Implement polymorphism Conditions
1. The parent class has a method modified with the virtual keyword.
2. The subclass must have a method with the same name. The Override keyword is used.
3. Add N subclass objects to the parent class type set ..
4. iterative