eclipse JDK 1.6 中的 @override 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3619036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@override annotation in JDK 1.6
提问by Veera
I'm using 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. i.e. below code is wrong according to Eclipse.
我正在使用 JDK1.6。当我实现一个接口并在实现类中时,如果我@override
在我的函数名称之前给出,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. Does it mean that JDK1.6 does not require us to prefix the @override
annotation anymore?
如果我删除@Override
注释,则代码编译正常。是不是说JDK1.6不再要求我们在@override
注解前加前缀了?
回答by Gennadiy
You probably need to set the compiler compliance level in eclipse. This can be found in Window->Preferences->Java->Compiler
您可能需要在 Eclipse 中设置编译器合规性级别。这可以在 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.
编辑:如果您已将这些设置为默认值以外的任何其他内容,请同时检查每个项目的编译器合规性级别。
回答by Colin Hebert
@Overidde works on method implementation since java 1.6.
@Overidde 从 java 1.6 开始处理方法实现。
Resources :
资源 :
- Sun's forums - Java Programming - Should @Override apply to implementation of interface/abstract methods?
- dertompson.com - @Override specification changes in Java 6
- The Former Weblog of Peter Ahé - @Override snafu
- Sun 的论坛 - Java 编程 - @Override 是否应该应用于接口/抽象方法的实现?
- dertompson.com - Java 6 中的 @Override 规范更改
- Peter Ahé 的前博客 - @Override snafu
On the same topic :
在同一主题上:
回答by kanaparthikiran
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. Another way is Right Click on Project-> Properties ->Java Compiler ->JDK Compliance ->Select JDK1.6 minimum, click apply.
Java Compiler 设置可以根据您选择的配置在多个位置,一种方法是 Window->Preferences->Java->Compiler,如果设置为某个早期版本,则将其更改为 1.6 最小值。另一种方法是右键单击项目->属性->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 添加到类路径,它将解决问题。
回答by borchvm
The @Override annotation changed in Java 1.6 version. In Java 1.5, the compiler didn't allow @Override annotation on implemented interface methods, from 1.6 it does.
@Override 注释在 Java 1.6 版本中发生了变化。在 Java 1.5 中,编译器不允许在实现的接口方法上使用 @Override 注释,而从 1.6 开始就允许了。
You must change java compiler version in properties project -> Java Compiler
您必须在属性项目 -> Java 编译器中更改 Java 编译器版本
回答by Noel M
It sounds like your compiler is set for Java 5, when @Override
on interfaces wasn't allowed.
听起来您的编译器是为 Java 5 设置的,@Override
但不允许使用接口。
回答by Riaan Cornelius
JDK1.6 definitely supports it. I'm not sure why you would have issues.
JDK1.6 肯定支持。我不确定你为什么会有问题。
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. Maybe you are compiling against an older JDK?
你看到什么错误?我唯一能想到的是确保您在项目设置中使用正确的 JDK。也许您正在针对较旧的 JDK 进行编译?
回答by Manuel Selva
No the @Override annotation is still used. You should check that the contextDestroyed
method is really present in the ServletContextListener
interface, and check the imported package for this interface.
不,仍然使用@Override 注释。您应该检查该contextDestroyed
方法是否确实存在于ServletContextListener
接口中,并检查该接口的导入包。