it is possible that the code of A.foo is executed during evaluation of (new B{}).foo{} in the following?
Class A{
Public foo(){…}
}
Class B extends A {
Public foo(){…}
}
yes or no?

解决方案 »

  1.   

    no, distinguish A from B, it's not the constructor, just have a tryclass A {

    public A(){
    System.out.println("A constructor");
    }

    public void foo() {
    System.out.println("A");
    }

    }class Test extends A {
    public void foo() {
    System.out.println("B");
    }

    public static void main(String[] args) {
    new Test().foo();
    }
    }
      

  2.   

    No,unless you call super.foo() explicitly.
      

  3.   

    如果你在Class B的foo()里有super.foo() ,那答案就是yes,会执行父类的方法
    不然的话,就被重写掉了,不会调用父类的
      

  4.   

    我只要个答案,yes or no
    这里回答的各不相同,谁能确定答案?
      

  5.   

    不是回答了嘛,答案是yes
    你的题目是问有没有可能,那就是有可能的
      

  6.   

    调用父类中的构造器super;
    如不显示调用会掉用父类的默认构造器