java在子类中,调用父类中被覆盖的方法

在java中,子类中调用与父类同名的方法(即父类中被覆盖的方法)用super来调用即可,下面是示例:

子类父类的定义

public class b
{
        void show() {
            System.out.println("b");
        }


}
    
public class c extends b
{
    
    void show() {
        System.out.println("c");
    }
    void showc() {
        super.show();
        show();
    }
}

在main执行

package lei;

public class a {

    public static void main(String[] args) {
        
        c x=new c();
        
        x.showc();
        
    }

    
    
    

}

执行结果

猜你喜欢

转载自www.cnblogs.com/liuleliu/p/11747946.html