class Father{
public void a(){
System.out.println("father'a()");
}
public void b(){
System.out.println("father'b()");
a();
}
}class Son extends Father{
public void a(){
System.out.println("son'a()");
super.a();
}
public void b(){
System.out.println("son'b()");
super.b();
}
}public class Demo{
public static void main(String[] args){
Father son = new Son();
son.b();
}
}

解决方案 »

  1.   

    son'b()
    father'b()
    son'a()
    father'a()
    应该是这样的吧
      

  2.   

    son'b()
    father'b()
    son'a()
    father'a()
      

  3.   

    为什么呢?能不能说下方法调用过程啊?
    为什么当调用了super.b()后,会调用子类的a(),而不是父类的a()?
      

  4.   

    class Father{
    // step 4
        public void a(){
            System.out.println("father'a()");
        }
        
        // step2
        public void b(){
            System.out.println("father'b()");
            
            // since instance of son --> son.a();
            a();
        }
    }class Son extends Father{
    // step3
        public void a(){
            System.out.println("son'a()");
            
            // a() of father 
            super.a();
        }
        
        // step1
        public void b(){
            System.out.println("son'b()");
            
            // b() of father 
            super.b();
        }
    }public class Demo{
        public static void main(String[] args){
            Father son = new Son();
            son.b();
        }
    }稍微解释了一下。;-)
      

  5.   

    为什么:a();会调用Father 的儿子呢?
      

  6.   

    错怪kakagui了 
    包歉 !
      

  7.   

    貌似,对象是b,a()找到的就是当前对象中的a()方法。
      

  8.   

    那怎么打印出:
    son'b()
    father'b()
    father'a()
    来呢?就是说当调用到super.b()时怎样让它调用Father的a()而不是Son覆盖后的a()呢?
      

  9.   

    class Father{
        public void a(){//4
            System.out.println("father'a()");
        }
        public void b(){
            System.out.println("father'b()");//3
            a();
        }
    }class Son extends Father{
        public void a(){//3
            System.out.println("son'a()");
            super.a();
        }
        public void b(){
            System.out.println("son'b()");//2
            super.b();
        }
    }public class Demo{
        public static void main(String[] args){
            Father son = new Son();
            son.b();//1    }
    }
      

  10.   

    class Father{
      public void a(){//5
      System.out.println("father'a()");
      }
      public void b(){
      System.out.println("father'b()");//3
      a();
      }
    }class Son extends Father{
      public void a(){//4
      System.out.println("son'a()");
      super.a();
      }
      public void b(){
      System.out.println("son'b()");//2
      super.b();
      }
    }public class Demo{
      public static void main(String[] args){
      Father son = new Son();
      son.b();//1  }
    }
      

  11.   

    Hi all, 
          This issue is about the difference between override and hidding method.I found some info in oracle's web.[url=http://docs.oracle.com/javase/tutorial/java/IandI/override.html]
          It means that the sun instance class  method a() hidding the father class  method a().So if you get a instance Son,it just call the method in class Son ,except applying super. 
          Sorry for my poor english .I just want to practise it. If I have any faults, please contact me.
    Thanks.
      

  12.   

    Get it!
    Thank you !
      

  13.   

    这是Thinking in java 中的一句话:“当创建了一个导出类(子类)的对象时,该对象包含了一个基类的子对象。这个子对象与你用基类直接创建的对象是一样的。二者的区别在于,后者来自于外部,而基类的子对象被包装在导出类对象内部。”
    我想大家看了这句话应该能明白为什么了,至于更底层更详细的具体细节我想还是以后再说吧!
      

  14.   

    首先调用Son中的void b() 打出son'b(),然后执行super.b()这样就调用Father中的void b()打出father'b(),然后执行a()这里就涉及到多态了,根据生成的对象调用相应的方法而不是根据引用的类型,这里的son是父类的一个引用,而实际的对象是一个Son对象
      

  15.   

    多态问题!你后边NEW出来的是一个SON 的对像,在这个对象里面调用a()时,就应该调用SON对像中的a()方法。
      

  16.   

    24、25楼正解。典型的多态,主要是看son对象到底是儿子类的还是父类的,一开始认为是子类的,调用了子类的b()方法。
      

  17.   

    这是哪本java初级教程的例题。
    其实就是叫你理解继承的概念,
    这个程序重在理解。
    运行结果不重要。