Executes the corresponding constructor of the parent class first, and then executes the current constructor.
About the execution order of constructors and parent class constructors for subclass objects
The following is transferred from: http://blog.csdn.net/todototry/article/details/4941484
Principle:
1, the constructor of any class, must be public, in order to derive sub-class
class Base
{
private int i;
Public Base (int i)
{
this.i = i;
Console.WriteLine ("Base constructor!");
}
}
class a:base
{
public A ()
{
Console.WriteLine ("Child");
}
}
static void Main ()
{
A = new A ();
}
A when instantiating an object construct, because there is no constructor specified out of the parent class, it calls the parameterless constructor, and the base class has no parameterless constructor, and the default constructor overrides the defined constructor, and constructs an error
2, such use:
class Base
{
private int i;
Public Base (int i)
{
this.i = i;
Console.WriteLine ("Base constructor!");
}
}
Class A:base
{
Public A (): Base (6)
{
Console.WriteLine ("Child");
}
}
static void Main ()
{
A = new A ();
}
C # for base classes can only be constructed with the base keyword, because there is no multiple inheritance in C #, only a single inheritance, so a fixed name represents the base class is sufficient
Base () after the constructor