C # class variables point to the instantiation of a derived class Object 1. hierarchy (for constructors)
The constructor of the class to be instantiated Cannot initialize the class, but must also call the constructor in the base class. Therefore, the hierarchical structure is introduced for construction.
The hierarchy is constructed by first finding the constructor of the class to be instantiated, then finding and default the constructor of its parent class, and then finding the system. object base class constructor, when there is no class above. Execute the constructor from the top-level class.
2. object initialization sequence
Subclass attributes or fields -----> base class constructor -----> subclass Constructor
A) variable first and then constructor. The variable is initialized first, and then the constructor is executed.
B) First static and then instantiated. When a class is accessed, the static variables and constructors are initialized first. Then the object's instantiated variables and constructors are initialized.
C) First, the derived class and then the base class. For variables and static constructors, the derived object is initialized before the base object. for example, Class C is derived from class B and class B is derived from Class A, then the initialization order of variables and static constructors is C-B-A.
D) Besides the instance constructor. For the instance constructor, the base class constructor is executed before the derived class constructor, And the instance constructor is executed in the order of A-B-C.
E) do not assume the order of variables. Fields is initialized in sequence based on their declaration in the source file. However, since programmers and tools can arrange variable declarations at will, you should not initialize the variables in any special order.
F) construct the virtual method in two stages. Avoid calling Virtual Methods From a constructor. if you need to call some virtual methods when initializing an object, you should use the two-phase building in the complete structure of the object, and then call the initialization method of the constructed object.
3. Root = new child (); this method is actually Object-Oriented Knowledge.
Of course, the base class object cannot access the member of the derived class. Although it is actually a child object, its type is parent. After the conversion, access to the member of the derived class is blocked.
The root object ignores other derived methods of the Child class, and only the override method is affected.