public class Test{
   public static Foo f=new Foo();
   public static Foo f2;
   public static Bar b=new Bar();
 public static void main(String arg0[]){
  for(int x=0;x<6;x++){
     f2=getFoo(x);
     f2.react();
  }
 }
 static Foo getFoo(int y){
  if(0==y%2){
    return f;
   }else{
    return b;
  }
 }
}
class Bar{
  void react(){System.out.println("Bar");}
}
class Foo extends Bar{
  void react(){System.out.println("Foo");}
}
what is the result?
A.Bar Bar Bar Bar Bar Bar 
B.Foo Bar Foo Bar Foo Bar 
C.Foo Foo Foo Foo Foo Foo 
D.Compilation fails
E.An exception is thrown at runtime请大家给个答案和理由

解决方案 »

  1.   

    D static Foo getFoo(int y){ 中return b;
    基类Bar不能向下转换Foo
      

  2.   

    getFoo的返回类型更改为Bar并把f2的类型申明为Bar应该就完全正常了。
    :)
      

  3.   

    我把
    1....getFoo的返回类型更改为Bar并把f2的类型申明为Bar
    2....react()都改为静态 
    请问下结果
      

  4.   

    [D static Foo getFoo(int y){ 中return b;
    基类Bar不能向下转换Foo]同意这个观点
      

  5.   

    [D]
    static Foo getFoo(int y){中return b;
    基类Bar不能向下转换Foo]同意这个观点