Do not know that the use of inheritance in the process of wood has encountered a call to the constructor function is not as we expected to execute it? In general, this problem often occurs because a base class in the class inheritance structure is not instantiated correctly, or does not properly provide information to the base class constructor, and it is more beneficial to solve such problems if you understand what happens at this stage of the object life cycle .
In order to instantiate a derived class, you must first instantiate its base class. Instead, instantiate the base class. It is also necessary to instantiate the base class of this base class, so that until the System.Object (all classes are followed) is instantiated, the result is always called System.Object.Object (), regardless of the constructor used to instantiate a class.
The following example shows the order of execution:
Base class:
Public class MyBaseClass { public mybaseclass () { Console.WriteLine ("I am MyBaseClass () "); } Public MyBaseClass (int i) { Console.WriteLine ("I am mybaseclass (int i) "); } }
Derived classes:
PublicMyderivedclass () {Console.WriteLine ("I am MYDERIVEDCALSS ()"); } PublicMyderivedclass (inti) {Console.WriteLine ("I am myderivedclass (int i)"); } PublicMyderivedclass (intIintj) {Console.WriteLine ("I am Myderivedclass (int i,int j)"); }
Next we instantiate the Myderivedclass in the main function with a constructor with no arguments:
New Myderivedclass ();
To run the program, the console output is as follows:
As you can see from the results, the execution sequence is the function of the base class construct, and then the constructor of the derived class, that is,
1. Execute the System.Object.Object () constructor (Object is special, all classes of the base class, generally can not be considered, but to know that it is also executed)
2. Execute the Mybaseclass.mybaseclass () constructor
3. Execute the Myderivedclass.myderivedclass () constructor
If we instantiate Myderivedclass with a constructor with one parameter:
New Myderivedclass (4);
To run the program, the console output is as follows:
You can see the following order of execution:
1. Execute the System.Object.Object () constructor
2. Execute the Mybaseclass.mybaseclass () constructor
3. Execute myderivedclass.myderivedclass (int i) constructor
Similarly, if we instantiate Myderivedclass with a constructor with two parameters
New Myderivedclass (4,8);
To run the program, the console output is as follows:
You can see the following order of execution:
1. Execute the System.Object.Object () constructor
2. Execute the Mybaseclass.mybaseclass () constructor
3. Execute Myderivedclass.myderivedclass (int i,int J) Constructor
Most of the time this works, but sometimes we need more control over what's happening. For example, we want the order of execution as shown below:
1. Execute the System.Object.Object () constructor
2. Execute mybaseclass.mybaseclass (int i) constructor
3. Execute Myderivedclass.myderivedclass (int i,int J) Constructor
Using this order, the code using the int i parameter can be put into mybaseclass (int i), myderivedclass (int i,int j) only need to handle int J (assuming the int i parameter in MyBaseClass and Myderivedclass meaning is the same)
To do this, you only need to use the constructor initializer to place the code after the colon of the method definition, such as specifying the constructor of the base class used in the constructor of the derived class, as follows:
Public Myderivedclass (int i,intbase(i) { Console.WriteLine ("I Am Myderivedclass (int i,int j)");
Where the base keyword specifies that a constructor with the specified parameters is used during instantiation. The int parameter is used here, whose value is passed to the Myderivedclass constructor by I, so the mybaseclass (int i) is used, so MyBaseClass () is not called, and we re-execute the instantiation code for the next two arguments. It can be seen that the result of the implementation is true:
In addition to the base keyword, you can use the This keyword as the constructor initializer, which specifies that the instantiation process uses a non-default constructor for the current class before invoking the specified constructor. For example:
Public Myderivedclass ():This (5,6) { Console.WriteLine ("I am MYDERIVEDCALSS ()"); }
Instantiated using the MYDERIVEDCALSS () constructor, the order of execution is:
1. Execute the System.Object.Object () constructor
2. Execute mybaseclass.mybaseclass (int i) constructor
3. Execute Myderivedclass.myderivedclass (int i,int J) Constructor
4. Execute the Myderivedclass.myderivedclass () constructor
In summary, no matter what constructor is used on the derived class (the default or is not the default), the default constructor for the base class is called first unless explicitly specified, such as with the base keyword.