The default constructor here refers to the system default parameterless constructor without writing the constructor
1. When a constructor is not written by itself in the base class, the default constructor of the base class is called by the derived class
For example:
?
123456789101112131415161718 |
public class MyBaseClass
{
}
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
Console.WriteLine(
"我是子类无参构造函数"
);
}
public MyDerivedClass(
int i)
{
Console.WriteLine(
"我是子类带一个参数的构造函数"
);
}
public MyDerivedClass(
int i,
int j)
{
Console.WriteLine(
"我是子类带二个参数的构造函数"
);
}
}
|
When instantiating a derived class, call the base class default constructor
2. When a constructor is written in a base class, the derived class does not specify which constructor the call constructs, it looks for the parameterless constructor, and if none is an error, and no matter which constructor in the derived class is called, it is the base class constructor that looks for the parameterless, not the parameter match.
For example:
?
12345678910111213141516171819202122 |
public class MyBaseClass
{
public MyBaseClass(
int i)
{
Console.WriteLine(
"我是基类带一个参数的构造函数"
);
}
}
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
Console.WriteLine(
"我是子类无参构造函数"
);
}
public MyDerivedClass(
int i)
{
Console.WriteLine(
"我是子类带一个参数的构造函数"
);
}
public MyDerivedClass(
int i,
int j)
{
Console.WriteLine(
"我是子类带二个参数的构造函数"
);
}
}
|
Error when instantiating a derived class
3. A constructor is written in a base class, and a derived class can specify a constructor that calls the base class, using the base keyword.
For example
?
12345678910111213141516171819202122 |
public class MyBaseClass
{
public MyBaseClass(
int i)
{
Console.WriteLine(
"我是基类带一个参数的构造函数"
);
}
}
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass() :
base
(i)
{
Console.WriteLine(
"我是子类无参构造函数"
);
}
public MyDerivedClass(
int i) :
base
(i)
{
Console.WriteLine(
"我是子类带一个参数的构造函数"
);
}
public MyDerivedClass(
int i,
int j) :
base
(i)
{
Console.WriteLine(
"我是子类带二个参数的构造函数"
);
}
}
|
When you instantiate a constructor with a parameter that is used when deriving a class, you do not get an error because he specifies the constructor of the base class.
4, if the constructor in the base class does not contain a parameterless constructor, the constructors in the derived class must all specify the base class constructor for the call, or else an error
For example
?
12345678910111213141516171819202122 |
public class MyBaseClass
{
public MyBaseClass(
int i)
{
Console.WriteLine(
"我是基类带一个参数的构造函数"
);
}
}
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
Console.WriteLine(
"我是子类无参构造函数"
);
}
public MyDerivedClass(
int i) :
base
(i)
{
Console.WriteLine(
"我是子类带一个参数的构造函数"
);
}
public MyDerivedClass(
int i,
int j)
{
Console.WriteLine(
"我是子类带二个参数的构造函数"
);
}
}
|
At this point the compilation will not pass
I hope this article is helpful to everyone's C # programming.
Derived classes in C # Call base class constructor usage analysis