学习是一个不断回归的过程,做过一些项目,再去读一些书籍,发现有些概念忽然之间竟能理解得更加透彻,真是让人非常快乐,你也希望我快乐,不是么?
继承关系如下:
public class MyBaseClass
{
public MyBaseClass()
{
Console.WriteLine("MyBaseClass()");
}
public MyBaseClass(int i)
{
Console.WriteLine("MyBaseClass(int {0})", i);
}
}
public class MyDerivedClass : MyBaseClass
{
// 调用此构造函数前,先调用本身的(int i, int j)构造函数
public MyDerivedClass() : this(2,3)
{
Console.WriteLine("MyDerivedClass()");
}
public MyDerivedClass(int i)
{
Console.WriteLine("MyDerivedClass(int i)");
}
// 调用此构造函数前,先调用父类的(int i)构造函数
public MyDerivedClass(int i, int j) : base(5)
{
Console.WriteLine("MyDerivedClass(int {0}, int {1})",i,j);
}
}
{
public MyBaseClass()
{
Console.WriteLine("MyBaseClass()");
}
public MyBaseClass(int i)
{
Console.WriteLine("MyBaseClass(int {0})", i);
}
}
public class MyDerivedClass : MyBaseClass
{
// 调用此构造函数前,先调用本身的(int i, int j)构造函数
public MyDerivedClass() : this(2,3)
{
Console.WriteLine("MyDerivedClass()");
}
public MyDerivedClass(int i)
{
Console.WriteLine("MyDerivedClass(int i)");
}
// 调用此构造函数前,先调用父类的(int i)构造函数
public MyDerivedClass(int i, int j) : base(5)
{
Console.WriteLine("MyDerivedClass(int {0}, int {1})",i,j);
}
}
执行MyDerivedClass mc = new MyDerivedClass();
结果如下:
System.Object.Object()
MyBaseClass(int 5)
MyDerivedClass(int 2, int 3)
MyDerivedClass()