为什么此转换不会生成编译器警告?

发布于 2024-12-29 08:00:53 字数 854 浏览 0 评论 0原文

为什么以下 java 代码不会生成编译器警告,提示“从 SuperClass 到 SomeBaseClass 的不安全转换”之类的内容?

public abstract SuperClass
{
    static SuperClass create()
    {
        return new AnotherBaseClass();
    }

    private static class SomeBaseClass extends SuperClass
    {
        void print()
        {
            System.out.println("Hello World");
        }
    }

    private static class AnotherBaseClass extends SuperClass
    {
    }

    public static void main(String[] args)
    {
        SomeBaseClass actuallyAnotherClass = (SomeBaseClass)SuperClass.create();
        actuallyAnotherClass.print();
    }
}

我在Windows机器上使用了jdk1.6.0_25/bin/javac。 Eclipse Helios 也没有对此发出警告。

相反,它会导致运行时异常:

线程“main”中的异常 java.lang.ClassCastException:SuperClass$AnotherBaseClass 无法转换为 SuperClass$SomeBaseClass

Why doesn't the following java code generate a compiler warning saying something like "Unsafe cast from SuperClass to SomeBaseClass"?

public abstract SuperClass
{
    static SuperClass create()
    {
        return new AnotherBaseClass();
    }

    private static class SomeBaseClass extends SuperClass
    {
        void print()
        {
            System.out.println("Hello World");
        }
    }

    private static class AnotherBaseClass extends SuperClass
    {
    }

    public static void main(String[] args)
    {
        SomeBaseClass actuallyAnotherClass = (SomeBaseClass)SuperClass.create();
        actuallyAnotherClass.print();
    }
}

I used jdk1.6.0_25/bin/javac on a Windows machine. Eclipse Helios doesn't warn about it either.

Instead it results in a runtime exception:

Exception in thread "main" java.lang.ClassCastException: SuperClass$AnotherBaseClass cannot be cast to SuperClass$SomeBaseClass

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

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

发布评论

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

评论(3

Smile简单爱 2025-01-05 08:00:53

Javac 仅在使用泛型时警告不安全的强制转换。在这里,编译器相信你知道你在做什么:)

Javac only warns for unsafe casts when using generics. Here the compiler trusts you to know what you're doing :)

掀纱窥君容 2025-01-05 08:00:53

实际上,如果根本不可能进行强制转换,编译器会抛出错误,例如,如果 create() 方法返回类型为 AnotherBaseClass 而不是 SuperClass< /代码>。

由于它返回一个SuperClass,编译器不知道实际返回什么——它也可能返回一个SomeBaseClass。因此,它必须相信您知道如何处理该演员。

编辑

要在转换时收到警告,您可以尝试使用代码分析工具,例如 Checkstyle。
但请注意,这些工具很可能无法或不检查类层次结构,因此可能只能警告一般使用的(非原始)强制转换。因此,如果您使用需要强制转换的库(例如,如果您使用尚不支持泛型的 apache commons 集合),您将收到大量警告。

毕竟,编程仍然是一门艺术,您仍然需要知道自己在做什么。

Actually, the compiler would throw an error if the cast was not possible at all, e.g. if the create() methods return type would be AnotherBaseClass instead of SuperClass.

Since it returns a SuperClass the compiler doesn't know what will actually be returned - it could as well return a SomeBaseClass. Thus it has to trust that you know what you do with that cast.

Edit:

To get a warning when casting you might try and employ a code analysis tool like Checkstyle.
Note, however, that those tools most likely can't or don't check the class hierarchy and thus might only be able to warn about (non-primitive) casts being used in general. Thus if you use a library that requires casts (e.g. if you're using apache commons collections which doesn't support generics yet) you'd get a lot of warnings.

After all, programming is still an art and you still need to know what you're doing.

醉殇 2025-01-05 08:00:53

这不是编译器警告。当尝试将 AnotherBaseClass 对象转换为 SomeBaseClass 时,它在运行时失败。

That's not a compiler warning. It has failed at runtime while trying to cast AnotherBaseClass object to SomeBaseClass.

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