天天看點

一段有意思的代碼

package ex01.pyrmont;

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

public class Father {

    public void a(){
        System.out.println("a from father");
        b();
    }
    
    public void b(){
        System.out.println("b from father");
    }
}
           

執行後會輸出什麼,為什麼?

答案如下

a from son

a from father

b from son

如果把Son中的方法b去掉,會怎麼樣呢?

a from son

a from father

b from father