class FatherProp {   
public FatherProp() {
        System.out.println("FatherProp is construct");
    }}
class Father {
    FatherProp SonProp = new FatherProp();
    public Father() {
        System.out.println("Father is construct");
    }}
class SonProp {
    public SonProp() {
        System.out.println("SonProp is construct");
    }}
public class Son extends Father {
    SonProp r = new SonProp();
    public Son() {
        System.out.println("Son is construct");
    }
    public static void main(String[] args) {
        new Son();
    }}
这个程序的调用顺序是什么的,为什么?请大家帮着解释一下啊

解决方案 »

  1.   

    main的主函数new了一个son对象..类son又有一个SonProp类型的对象r..所以System.out.println("SonProp is construct"); 这条语句先于System.out.println("Son is construct"); 被打印..再看回public class Son extends Father..是继承于Father类的.编译器会自动插入super调用基类的构造函数..所以语句System.out.println("Father is construct"); 先于上面的两条语句打印输出..最后类Father又有个FatherProp的对象..所以会打印 System.out.println("FatherProp is construct"); 输出的顺序为:
    FatherProp is construct
    Father is construct
    SonProp is construct
    Son is construct