请考虑以下代码:
public class RecursiveConstructor { //When this constructor is called public RecursiveConstructor():this(One(),Two()) { Console.WriteLine("Constructor one. Basic."); } public RecursiveConstructor(int i,int j) { Console.WriteLine("Constructor two."); Console.WriteLine("Total = " + (i+j)); } public static int One() { return 1; } public static int Two() { return 2; } }
public class RecursiveConstructorTest { public static void Main() { RecursiveConstructor recursiveConstructor = new RecursiveConstructor(); Console.ReadKey(); } }
结果
Constructor two.
Total = 3
Constructor one. Basic.
为什么第二个构造函数首先运行?
我明白,在链式构造函数中,我们首先称之为基类构造函数,然后使我们的方式备份在链中,但是当构造函数保存在同一个类中时,为什么我们仍然看到这种行为,首先调用额外的构造函数?