@重写混乱

发布于 2024-09-25 12:06:52 字数 549 浏览 6 评论 0原文

我已经使用 JDK1.6 在 Eclipse Helios 中创建了一个项目/类文件。我让 Eclipse 生成接口的实现类的代码。

public interface Foo {
    void bar();
}

public class FooImpl implements Foo {
    @Override
    public void bar() {
    }
}

到目前为止,一切都很好。现在,由于某种原因,我已将项目导入到具有 JDK 1.5 的 Eclipse 中,并且看到错误消息 FooImpl 类型的方法 bar() 必须重写超类方法快速修复删除“@Override”注释

经过谷歌搜索后,我知道有类似 OVERRIDE_SNAUF 的东西 - 6.0 Java 编译器在哪里更新为允许在接口方法实现上使用 @Override。

i've create a project/class file in Eclipse Helios using JDK1.6. I had let the eclipse to generate the code for the implementation class of an Interface.

public interface Foo {
    void bar();
}

public class FooImpl implements Foo {
    @Override
    public void bar() {
    }
}

So far so good. Now for some reason, I've imported the project in an Eclipse which has JDK 1.5 and I see error message The method bar() of type FooImpl must override a superclass method with a quick fix to remove '@Override' annotation.

After googling, I got to know there is something like OVERRIDE_SNAUF - where 6.0 Java compiler was updated to allow @Override on interface method implementations.

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

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

发布评论

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

评论(3

十年不长 2024-10-02 12:06:52

@Override 非常适合在编译阶段检查覆盖语法,因此它也适用于接口,原因与我猜测相同。

@Override is good for checking override syntax in compile stage, so it is also applied to interface for same reason I guess.

酒儿 2024-10-02 12:06:52

找不到问题,但是 - 是的,你是对的,Java 5 中不允许使用 @Override 注释来注释重写的接口方法。因此,如果需要,您必须删除这些注释使用 Java 5 编译代码。

Can't find a question but - yes, you're right, the @Override annotation was not allowed to annotate overriden interface methods in Java 5. So you'll have to remove those annotations if you want to compile the code with Java 5.

只是偏爱你 2024-10-02 12:06:52

即使您有 jdk 1.6,您也可以将编译器选项设置为合规性级别 1.5。这样,eclipse生成的代码将与java 1.5兼容(它不会在实现接口的方法上添加@Override注释)。我认为您实际上可以将其设置为根本不添加 @Override 注释。

嗯,实际上比这更复杂一点。为了完全兼容,您应该安装 JDK 1.6 和 JDK 1.5,并将每一个设置为其合规级别的默认 JDK。这是确保代码完全向后兼容的最佳方法。

在首选项窗口(常规或特定项目)中,转到 Java-> 编译器并在那里设置合规性级别。同样在Java->Installed JREs中添加JDK 1.5,然后在Java->Installed JREs->Execution Environments中点击J2SE-1.5,勾选新安装的JDK 1.5。如果您的项目或工作区设置为使用该合规性级别,这可确保 eclipse 将使用编译器行为以及 JDK 1.5 附带的标准库。

You can set your compiler options to compliance level 1.5 even if you have jdk 1.6. That way the code generated by eclipse will be compatibile with java 1.5 (it won't add the @Override annotation on methods that implement an interface). I think you can in fact set it to never put an @Override annotation at all.

Well, actually it's a tad more complicated than that. For full compatibility you should install both JDK 1.6 and JDK 1.5 and set each one as the default JDK for their compliance level. That's the best way to ensure full backwards compatibility of your code.

In the preferences window (general, or for a specific project) go to Java->Compiler and set the compliance level there. Also in Java->Installed JREs add JDK 1.5, and then in Java->Installed JREs->Execution Environments click on J2SE-1.5 and check the newly installed JDK 1.5. This ensures that eclipse will use the compiler behaviour as well as the standard library that comes with JDK 1.5 if your project or workspace is set to use that compliance level.

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