JAVA-Interface and Inheritance (5) Hidden

father

package charactor;
public class hero {
    
    
	public int hp;
	public String name;
	public static void battlewin(){
    
    
		System.out.println("战斗结束,获得胜利");
	}
}

Subclass hides the class method of the parent class

package charactor;
  
public class ADHero extends Hero implements AD{
    
    
  
    @Override
    public void physicAttack() {
    
    
        System.out.println("进行物理攻击");
    }
     
    //隐藏父类的battleWin方法
    public static void battleWin(){
    
    
        System.out.println("ad hero battle win");
    }   
     
    public static void main(String[] args) {
    
    
        Hero.battleWin();
        ADHero.battleWin();
    }
  
}

Exercise-Hide ⭐⭐⭐

Hero h =new ADHero();

h.battleWin(); //battleWin is a class method
h is a reference
to the parent class type but points to a subclass object
h.battleWin(); will call the parent class method? Or subclass method?
Answer: The method of the parent class will be called.

Guess you like

Origin blog.csdn.net/qq_17802895/article/details/108545105