为什么当用于两个不兼容的类时,“instanceof”会出错而不是返回“false”?

发布于 2024-10-08 02:27:44 字数 804 浏览 1 评论 0原文

我正在读这个:
http://java.sun.com/ docs/books/jls/third_edition/html/expressions.html#15.20.2

他们说:

考虑示例程序:

class Point { int x, y; }
类元素 { int 原子编号; }
类测试{
        公共静态无效主(字符串[] args){
                点 p = new Point();
                元素 e = new Element();
                if (e instanceof Point) { // 编译时错误
                        System.out.println("我明白你的意思了!");
                        p = (点)e; // 编译时错误
                }
        }
}

instanceof 表达式不正确,因为 Element 或其任何可能的子类(此处均未显示)的实例都不可能是 任何子类的实例>点

为什么这会导致错误,而不是简单地导致 instanceof 返回 false?

I'm reading this:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2

They say:

Consider the example program:

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
                }
        }
}

The instanceof expression is incorrect because no instance of Element or any of its possible subclasses (none are shown here) could possibly be an instance of any subclass of Point.

Why does this result in an error, rather than simply in instanceof returning false?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬心 2024-10-15 02:27:44

instanceof 检查是运行时检查。编译器能够在编译时(更早的时候)发现这个条件不正确,因此它告诉您它是错误的。永远记住,快速失败是一种很好的练习,它会节省你大量的时间和精力。

instanceof check is a runtime check. The compiler is able to discover that this condition is incorrect at compile time (much earlier), so it tells you that it is wrong. Always remember, that failing fast is a good practice, it will save you a lot of time and nerves.

七禾 2024-10-15 02:27:44

我会这么说,因为你在编译时就知道它永远不会是真的。因此,可以安全地假设这不是程序员的意思:)

但是,可能有更多 java 技术的解释。

I'd say, because you know at compile-time that it will never be true. Therefore, it's safe to assume this is not what the programmer meant :)

However, there probably is a more java-technical explanation.

蘑菇王子 2024-10-15 02:27:44

因为编译器知道 Element 不可能是 Point,所以会出现编译错误。

Because the compiler knows that is impossible to an Element be a Point, so you get an compilation error.

微凉 2024-10-15 02:27:44

因为继承树。如果A继承自B那么你可以写B的A实例

Integer i = 3;

System.out.println(i instanceof String); // compile time error

System.out.println(i instanceof Number); // true

System.out.println(i instanceof Object); // true

Because of the inheritance tree. if A inherited from B then you can write A instance of B

Integer i = 3;

System.out.println(i instanceof String); // compile time error

System.out.println(i instanceof Number); // true

System.out.println(i instanceof Object); // true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文