The default constructor in this article refers to the system default non-argument constructor without compiling the constructor.
1. When the base class does not write its own constructor, the derived class calls the default constructor of the base class by default.
Ex:
Public class MyBaseClass
{
}
Public class MyDerivedClass: MyBaseClass
{
Public MyDerivedClass ()
{
Console. WriteLine ("I Am a subclass without parameters constructor ");
}
Public MyDerivedClass (int I)
{
Console. WriteLine ("constructor with a parameter ");
}
Public MyDerivedClass (int I, int j)
{
Console. WriteLine ("constructor with two parameters in my subclass ");
}
}
When instantiating a derived class, the default constructor of the base class is called.
2. When a constructor is written in the base class, if the derived class does not specify the constructor to call, the constructor without parameters will be searched. If no constructor exists, an error is returned, in addition, no matter which constructor In the derived class is called, it is used to find the base class constructor without parameters, rather than parameter matching.
Ex:
Public class MyBaseClass
{
Public MyBaseClass (int I)
{
Console. WriteLine ("I Am a base class constructor with a parameter ");
}
}
Public class myderivedclass: mybaseclass
{
Public myderivedclass ()
{
Console. writeline ("I Am a subclass without parameters constructor ");
}
Public myderivedclass (int I)
{
Console. writeline ("constructor with a parameter ");
}
Public myderivedclass (int I, Int J)
{
Console. WriteLine ("constructor with two parameters in my subclass ");
}
}
An error is reported when the derived class is instantiated.
3. If constructors are written in the base class, you can specify a base class constructor In the derived class and use the base keyword.
Ex
Public class MyBaseClass
{
Public MyBaseClass (int I)
{
Console. WriteLine ("I Am a base class constructor with a parameter ");
}
}
Public class MyDerivedClass: MyBaseClass
{
Public MyDerivedClass (): base (I)
{
Console. WriteLine ("I Am a subclass without parameters constructor ");
}
Public MyDerivedClass (int I): base (I)
{
Console. WriteLine ("constructor with a parameter ");
}
Public MyDerivedClass (int I, int j): base (I)
{
Console. WriteLine ("constructor with two parameters in my subclass ");
}
}
When the constructor with a parameter is used to instantiate a derived class, no error is reported because it specifies the constructor of the base class.
4. If the constructor in the base class does not contain any constructor, the constructor In the derived class must specify all the called base class constructor; otherwise, an error occurs.
Ex
Public class mybaseclass
{
Public mybaseclass (int I)
{
Console. writeline ("I Am a base class constructor with a parameter ");
}
}
Public class myderivedclass: mybaseclass
{
Public myderivedclass ()
{
Console. WriteLine ("I Am a subclass without parameters constructor ");
}
Public MyDerivedClass (int I): base (I)
{
Console. WriteLine ("constructor with a parameter ");
}
Public MyDerivedClass (int I, int j)
{
Console. WriteLine ("constructor with two parameters in my subclass ");
}
}
At this time, compilation will fail
This article from CSDN blog, http://blog.csdn.net/cj205/archive/2008/12/05/3449828.aspx