C # class variables point to the instantiation of a derived class Object

Source: Internet
Author: User
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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.