On csdn today, someone asked about the execution process of a class and its derived classes, and asked the output result: 1 Using System;
2 Using System. Collections. Generic;
3 Using System. text;
4
5 Namespace Leleapplication1 {
6 Class Program {
7 Static Void Main ( String [] ARGs) {
8A= NewB ();
9Console. Readline ();
10}
11 }
12
13 Class A {
14 // When this constructor is called: x = 1, y = 0
15 Public A () {
16 // This function is overloaded in Class B, so the output x = 1, y = 0
17 Console. writeline ( " In the constructor of Class " );
18 Printfields ();
19 }
20 Public Virtual Void Printfields () {}
21 }
22 Class B: {
23 // The execution sequence of the derived class variable/static member is better than that of the base class.
24 // First variable, so x = 1, y = 0
25 Int X = 1 ;
26 Int Y;
27 // Then, call the base class constructor.
28 Public B () {
29 // The Class A constructor has been executed.
30 Y = - 1 ;
31 Console. writeline ( " In Class B Constructor " );
32 // In this case, x = 1, y =-1
33 Printfields ();
34 }
35 Public Override Void Printfields () {
36Console. writeline ("X = {0}, y = {1}", X, y );
37}
38 }
39 }
Here, let's forget the initialization sequence of C # objects and the initialization sequence of C ++ objects:
C #Object initialization
1.Variable first and then constructor. The variable is initialized first.,Then the constructor is executed.
2. first static and then instantiated. When a class is accessed , static variables and constructors are initialized first . the object's instantiated variables and constructors are initialized.
3. 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, C class derived from B class , B class derived from A class , C-B-A.
4. besides the instance constructor. For instance constructors , the base class constructor is executed before the derived class constructor ,
5. do not assume the order of variables. fields . however, , since the Program staff and tools can schedule variable declaration at will, , You should not initialize variables in any special order
6.The virtual method is constructed in two phases. Avoid calling Virtual Methods From a constructor.If you need to call some virtual methods when initializing an object,The two-phase building should be used in the complete construction of the object, and then the initialization method of the constructed object should be called.
C ++Constructor Call Sequence
1.If the class contains a member class, the constructor of the member class is called first;
2.Create the object of the derived class. The base class constructor is called first (also takes precedence over the member classes in the derived class );
3.If a base class constructor has multiple base classes, the calling sequence of the constructor is the sequence in which a class appears in the class derived table, rather than the sequence in the member initialization table;
4.If a member class object constructor has multiple Member class objects, the calling sequence of the constructor is the order in which objects are declared in the class rather than the order in the member initialization table;
5.A derived class constructor. As a general rule, a derived class constructor should not directly assign values to a base class data member, but pass the values to the appropriate base class constructor.,Otherwise, the implementation of the two classes becomes tightly coupled (Tightly coupled) Will be more difficult to correctly modify or extend the implementation of the base class. (The responsibility of the base class designer is to provide a set of appropriate base class constructor)