我正在阅读:http:
//java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2
他们说:
考虑示例程序:
class Point { int x, y; }
class Element { int atomicNumber; }
class Test {
public static void main(String[] args) {
Point p = new Point();
Element e = new Element();
if (e instanceof Point) { // compile-time error
System.out.println("I get your point!");
p = (Point)e; // compile-time error
}
}
}
该
instanceof
表达式不正确,因为没有 的实例Element
或其任何可能的子类(此处未显示)可能是 的任何子类的实例Point
。
为什么这会导致错误,而不是简单地instanceof
返回 false?
谢谢,
杰拉格