public class work2 {
public static void main(String[] args){
father A=new father();
son C=new son();
System.out.println("A="+C.f());
}
}
class father{
int i=1;
public int f(){
if(i==4)
return 0;
System.out.println(i+++"father");
return f();
}
}
class son extends father{
static int j=1;
public int f(){
System.out.println(j+++"son="+super.f());
return 0;
}
}

解决方案 »

  1.   

    我估计你对 return f(); 这个没有看清。这个返回时,并不是调用father的函数,不是自己调自己,而是运行的是son的的f().因为C就是son, 只要重名的,被override的都是调用子类的。
      

  2.   

    我猜一下答案,A1son1father
    0
    0
      

  3.   

    1father
    2father
    3father
    4son=0
    3son=0
    2son=0
    1son=0
    A=0
    这输出结果 ~~ 有可能困惑在于 father  调用的 f() 是son 里面的 ~~
      

  4.   

    public class work2 {
        public static void main(String[] args){
           // father A=new father();  delete
            son C=new son();
            System.out.println("A="+C.f());
        }
    }
    class father{
        int i=1;
        public int f(){
            if(i==4)
                return 0;
            System.out.println(i+++"father");
            return f();
        }
    }
    class son extends father{
        static int j=1;
        public int f(){
            System.out.println(j+++"son="+super.f());
            return 0;
        }
    }
      

  5.   


    public class work2 {
        public static void main(String[] args){
           // father A=new father();  delete
            son C=new son();
            System.out.println("A="+C.f());
        }
    }
    class father{
        int i=1;
        public int f(){
            if(i==4)
                return 0;
            System.out.println(i+++"father");
            return f();//这里做递归调用  f()调用的 是son里面的 f() 
        }
    }
    class son extends father{
        static int j=1;
        public int f(){
            System.out.println(j+++"son="+super.f());
            return 0;
        }
    }
      

  6.   

    为什么是这结果?
    为什么不是这结果:
    1father
    2father
    3father
    4son=0
    A=0
      

  7.   

    昨晚太冲忙了,不好意思,我预计结果是
    1father
    2father
    3father
    1son=0
    A=0