class Father {        public Father()
        {
                System.out.println("Father's Constructor!");
                f();
        }
        void f()
        {
                System.out.println("Father's function!");
        }
}
public class Son extends Father
{        public Son()
        {
                System.out.println("Son's Constructor!");
                f();
        }
        void f()
        {
                System.out.println("Son's function!");
        }
        public static void main(String[] args)
        {
                Son son = new Son();
        }
}
就上面的一段程序,输出的结果是:
Father's Constructor!
Son's function!
Son's Constructor!
Son's function!
这里,小弟不大明白:
本来子类的构造函数将构造函数的参数(这里无参),那么打印第一句Father's Constructor!没有问题,只是为什么接下来是打印Son's function!而不是Father's function!呢?

解决方案 »

  1.   

    那么打印第一句Father's Constructor!没有问题,
    ////
    同上面一样,这也是没有问题的 因为多态同样也要调用子类的函数不要在构造函数内调用可能多态的方法
      

  2.   

    对的啊,执行顺序是
    1、执行Father的构造函数
    2、在Father中调用 f() 函数
    3、执行Son的构造函数
    4、在Son中调用f()函数