@Override 编译错误,实现一个接口(eclipse jdk1.6.0_23 linux)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4995780/
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 compile error, implementing an interface (eclipse jdk1.6.0_23 linux)
提问by jsoc
I am getting compile errors in eclipse when using the @Override annotation for a class that is implementing an interface.
对实现接口的类使用 @Override 注释时,我在 Eclipse 中遇到编译错误。
Compiler compliance level is set to Java 6.0.
编译器合规性级别设置为 Java 6.0。
I am using the latest version of the 6.0 jdk.
我使用的是最新版本的 6.0 jdk。
Error: "The method {methodname} of type {classname} must override a superclass method"
错误:“{classname} 类型的方法 {methodname} 必须覆盖超类方法”
Same code works fine on mac with comparable configuration.
相同的代码在具有可比配置的 mac 上运行良好。
public interface ChannelIF {
...
public boolean canSendNarrowcast();
public boolean canSendBroadcast();
}
public class FacebookChannel implements ChannelIF
{
...
@Override
public boolean canSendNarrowcast() { return true; }
@Override
public boolean canSendBroadcast() { return true; }
}
回答by Steven Schlansker
Sounds like your method signatures don't match. They must be the same, including things such as thrown checked exceptions. Post your code!
听起来您的方法签名不匹配。它们必须相同,包括抛出的检查异常之类的东西。发布您的代码!
回答by Asaph
This feature is only valid in Java 6 and higher. I see you areusing jdk 1.6. That's good. Possible cause: You are compiling with -source 1.5
. Is this the case? If so, can you change it to -source 1.6
?
此功能仅在 Java 6 及更高版本中有效。我看到您正在使用 jdk 1.6。那挺好的。可能的原因:您正在使用-source 1.5
. 是这种情况吗?如果是这样,你能把它改成-source 1.6
吗?
回答by splean
In eslipse can use different versions of compilers.
在 eslipse 中可以使用不同版本的编译器。
See сonfiguration in eclipse Preference->Java->Compiler "Compiler compliance level". You must choose "1.6".
请参阅 eclipse Preference->Java->Compiler 中的 сconfiguration“编译器合规级别”。您必须选择“1.6”。
回答by pjaol
Check the Runtime library, the compiler settings are probably different
检查运行库,编译器设置可能不同
Right click on the project name, properties, Java Build Path, Libraries
右键单击项目名称、属性、Java 构建路径、库
Look at the version of JRE System Library, more than likely it's set to 1.5 or lower. Select JRE System library, then click remove Click Add Library, select JRE System Library, Next Then either Workspace Default, or
查看 JRE 系统库的版本,它很可能设置为 1.5 或更低。选择 JRE 系统库,然后单击删除 单击添加库,选择 JRE 系统库,下一步然后是 Workspace Default,或
回答by Jaime Garza
I just found that when using maven I needed to also modify the pom.xml to have the compiler plugin. I had all my settings properly specified, but I needed this one:
我刚刚发现在使用 maven 时我还需要修改 pom.xml 以拥有编译器插件。我已经正确指定了所有设置,但我需要这个:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
回答by jdowdell
I had this same problem for about an hour, and then it seemed to mysteriously solve itself. I wish I know what I did. I had started with workspace settings set for 1.6, unwittingly having project settings set to 1.5 compatibility. I discovered this and changed project compiler settings to 1.6, which resolved some errors, but not the @Override issue for interfaces. Some combination of the following fixed it:
我遇到了同样的问题大约一个小时,然后它似乎神秘地自行解决了。我希望我知道我做了什么。我已经开始将工作区设置设置为 1.6,不知不觉地将项目设置设置为 1.5 兼容性。我发现了这一点并将项目编译器设置更改为 1.6,这解决了一些错误,但没有解决接口的 @Override 问题。以下的一些组合修复了它:
- Changing the jre back from jdk to the jre that Eclipse custom installed (or somebody, wasn't me).
- Disabling project specific settings in the compiler section, and also clicking Reset to Defaults, on both project and workspace settings for compiler.
- Quitting Eclipse and rerunning it as Administrator (I copied eclipse into Program Files on Windows from a .zip, not from an installer; and I think it can't change its config files being a child of that folder without being run as administrator - but I could be dead wrong).
- 将 jre 从 jdk 改回 Eclipse 自定义安装的 jre(或某人,不是我)。
- 在编译器部分禁用项目特定设置,并在编译器的项目和工作区设置上单击重置为默认值。
- 退出 Eclipse 并以管理员身份重新运行它(我将 Eclipse 从 .zip 复制到 Windows 上的 Program Files 中,而不是从安装程序中;并且我认为它无法更改其配置文件作为该文件夹的子文件而无需以管理员身份运行 - 但是我可能完全错了)。
One fellow apparently solved the issue via complete uninstall and reinstall of all java/eclipse related components: http://p2p.wrox.com/book-beginning-android-application-development/87299-override-errors-despite-1-6-a.html
一个人显然通过完全卸载并重新安装所有与 java/eclipse 相关的组件解决了这个问题:http: //p2p.wrox.com/book-beginning-android-application-development/87299-override-errors-despite-1-6 -a.html
回答by Edub Kendo
So, I kept having this problem, and after running through the above solutions a few times, seeing that my version of Java was set to 1.7 all the way down, not using Maven, and so just quitting and restarting Eclipse a couple times as @jdowdell suggested, after which, it would seem to work until the next time I implemented one of these. I realized that when I was re-starting Eclipse it was prompting me to save my files and I realized that my interfaces had not been saved, so the original method, the one being overrode, didn't exist on disk. And this is why quitting and restarting Eclipse fixes the problem.
所以,我一直遇到这个问题,在运行了几次上述解决方案之后,看到我的 Java 版本一直设置为 1.7,没有使用 Maven,所以只是退出并重新启动 Eclipse 几次@ jdowdell 建议,在那之后,它似乎会一直工作到我下次实现其中一个。我意识到当我重新启动 Eclipse 时,它提示我保存我的文件,我意识到我的接口没有保存,所以原来的方法,被覆盖的方法,在磁盘上不存在。这就是为什么退出并重新启动 Eclipse 可以解决问题的原因。
tl;dr : Check that all your files are saved.
tl;dr :检查您的所有文件是否都已保存。
回答by CHowdappaM
I too have facing problem and just resolved. Change "Compiler compliance level" to 1.6 in Project->right click->properties->Java compiler.
我也遇到了问题,刚刚解决。在项目->右键单击->属性->Java编译器中将“编译器合规级别”更改为1.6。