写出下面程序的结果:
public class Test {
public static void main(String[] args) {
Child child = new Child();
}
}
class Parent{
Parent(){
System.out.println("construct parent");
}
}
class Child extends Parent{
private Delegatee delegatee = new Delegatee();
Child(){
System.out.println("construct child");
}
}
class Delegatee{
Delegatee(){
System.out.println("construct delegatee");
}
}
输出结果:
construct parent
construct delegatee
construct child
上面类之间的关系对应的UML图
继承的箭头指向父类是表示子类依赖父类, Child与Child的成员变量Delegatee是关联关系, Child依赖于Delegatee, 所以当new Child时, 先将执行其父类, 然后执行关联的Delegatee的构造方法, 最后再执行Child的构造方法