首页 > 代码库 > 一段有意思的代码

一段有意思的代码

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


一段有意思的代码