abstract class Animal
{
}
class Cat extends Animal{}
class Dog extends Animal{}
class Eagle{}
interface CanFly
{}
class Tester{
public static void main(String... args)
{
Eagle eagle = new Eagle();
System.out.println(eagle instanceof Animal); // compilation failure
System.out.println(eagle instanceof CanFly); // no compilation failure
}
}
我有点困惑为什么instanceof
与抽象类一起使用时要进行编译检查,而与Interface一起使用时要等到运行时。