【发布时间】:2012-04-27 07:07:12
【问题描述】:
如何检查某个类是否实现了接口? 当有:
Character.Gorgon gor = new Character.Gorgon();
如何检查gor是否实现Monster接口?
public interface Monster {
public int getLevel();
public int level = 1;
}
public class Character {
public static class Gorgon extends Character implements Monster {
public int level;
@Override
public int getLevel() { return level; }
public Gorgon() {
type = "Gorgon";
}
}
}
Gorgon 中的 getLevel() 方法是否被正确覆盖,所以它可以返回新的 gor 创建的 level?
【问题讨论】:
-
我认为你需要 instanceof 关键字。