JAVA: 先执行父类的构造函数,然后是引用对象的构造函数(必须有new声明实际类型),然后是自己的构造函数。
public class Test { public static void main(String[] args) { Child child = new Child(); }}
class Parent { Parent() { System.out.println("to construct Parent."); } }
class Child extends Parent { Child() { System.out.println("to construct Child."); } Delegatee delegatee = new Delegatee(); }
class Delegatee { Delegatee() { System.out.println("to construct Delegatee."); }}
结果是:
to construct Parent.to construct Delegatee.to construct Child.
而C#的构造函数执行顺序是:先引用对象,在父类,再子类
结果是to construct Delegatee.to construct Child.to construct Parent.