interface CanFight {
void fight();
}
interface CanSwim {
void swim();
}
interface CanFly {
void fly();
}
class ActionCharacter {
public void fight() {
System.out.println("Inside class fight");
}
}
class Hero extends ActionCharacter
implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
}
public class MultipleInterface {
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); // Treat it as a CanFight
u(h); // Treat it as a CanSwim
v(h); // Treat it as a CanFly
w(h); // Treat it as an ActionCharacter
}
}
如何JVM表現得像我不是從canFight
接口的實現方法,但它需要從ActionCharacter
類?它背後的邏輯是什麼?要點是我沒有定義方法fight()
,如果實現canFight()
接口必須完成。如何JVM對此有何反應
試試看! SO不是一個你可以簡單嘗試看的問題! – Varun 2013-03-25 06:17:28
當你嘗試時會得到什麼? – 2013-03-25 06:18:52