Java基础,接口
当我们实现某个接口时,必须重写其所有方法,但是一下程序么有实现 CanFight接口的void fight()方法,程序么有问题
interface CanFight {void fight();}
interface CanFly {void fly();}
interface CanSwim {void swim();}
class ActionCharacter {public void fight(){}}
class Hero extends ActionCharacter implements CanFight, CanFly, CanSwim {
@Override
public void swim() {}
@Override
public void fly() { }
}
public class Adventure {
public static void t(CanFight x){x.fight();}
public static void u(CanSwim x){x.swim();}
public static void v(CanFly x){x.fly();}
public static void w(ActionCharacter x){x.fight();}
public static void main(String[] args) {
Hero h = new Hero();
t(h);
u(h);
v(h);
w(h);
}
}
------解决方案--------------------interface CanFight {
void fight();}
interface CanFly {void fly();}
interface CanSwim {void swim();}
class
ActionCharacter {
public void fight(){}}
class Hero
extends ActionCharacter implements CanFight, CanFly, CanSwim {
------解决方案--------------------因为它的父类已经实现过了。。。
子类继承父类的时候会继承父类已有的所有方法。