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 the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cj205/archive/2008/12/05/3449828.aspx