简体   繁体   English

JDK 1.6 中的 @override 注释

[英]@override annotation in JDK 1.6

I'm using JDK1.6.我正在使用 JDK1.6。 When I implement an interface and in the implementing class, if I give @override before my function names, Eclipse throws an compilation error.当我实现一个接口并在实现类中时,如果我在我的函数名称之前给出@override ,Eclipse 会抛出一个编译错误。 ie below code is wrong according to Eclipse.即根据Eclipse,下面的代码是错误的。

public class SomeListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
       // code
    }
    /* other overridden methods here */
}

If I remove @Override annotation, then the code compiles fine.如果我删除@Override注释,则代码编译正常。 Does it mean that JDK1.6 does not require us to prefix the @override annotation anymore?这是否意味着 JDK1.6 不再要求我们为@override注释添加前缀?

You probably need to set the compiler compliance level in eclipse.您可能需要在 Eclipse 中设置编译器合规性级别。 This can be found in Window->Preferences->Java->Compiler这可以在 Window->Preferences->Java->Compiler 中找到

If the compiler preferences are still set to 1.5 the compiler will barf on the override annotation.如果编译器首选项仍设置为 1.5,编译器将禁止覆盖注释。

Edit: Also check compiler compliance level on a per project basis if you've set those to anything else than default.编辑:如果您已将这些设置为默认值以外的任何其他内容,请同时检查每个项目的编译器合规性级别。

The Java Compiler settings can be at multiple places based on the configuration You choose, One way is to Window->Preferences->Java->Compiler, change that to 1.6 minimum, if it was set to some earlier version. Java Compiler 设置可以根据您选择的配置在多个位置,一种方法是 Window->Preferences->Java->Compiler,如果设置为某个早期版本,则将其更改为 1.6 最小值。 Another way is Right Click on Project-> Properties ->Java Compiler ->JDK Compliance ->Select JDK1.6 minimum, click apply.另一种方法是右键单击项目->属性->Java编译器->JDK合规性->最低选择JDK1.6,点击应用。

After you make the changes, let the project build, it builds and take the changes into affect.进行更改后,让项目构建,它会构建并使更改生效。

If none of the above options work - Try adding the rt.jar to classpath, it will fix the problem.如果上述选项都不起作用 - 尝试将 rt.jar 添加到类路径,它将解决问题。

The @Override annotation changed in Java 1.6 version. @Override 注释在 Java 1.6 版本中发生了变化。 In Java 1.5, the compiler didn't allow @Override annotation on implemented interface methods, from 1.6 it does.在 Java 1.5 中,编译器不允许在实现的接口方法上使用 @Override 注释,而从 1.6 开始就允许了。

Java编译器

You must change java compiler version in properties project -> Java Compiler您必须在属性项目 -> Java 编译器中更改 Java 编译器版本

JDK1.6 definitely supports it. JDK1.6 肯定支持。 I'm not sure why you would have issues.我不确定你为什么会有问题。

What error are you seeing?你看到什么错误? The only thing I can think of is to make sure that you are using the correct JDK in your project settings.我唯一能想到的是确保您在项目设置中使用正确的 JDK。 Maybe you are compiling against an older JDK?也许您正在针对较旧的 JDK 进行编译?

No the @Override annotation is still used.不,仍然使用@Override 注释。 You should check that the contextDestroyed method is really present in the ServletContextListener interface, and check the imported package for this interface.您应该检查ServletContextListener接口中是否确实存在contextDestroyed方法,并检查该接口的导入包。

听起来您的编译器是为 Java 5 设置的,但不允许在接口上使用@Override

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM