16

在为实现接口的类使用 @Override 注释时,我在 Eclipse 中遇到编译错误。

编译器合规级别设置为 Java 6.0。

我正在使用最新版本的 6.0 jdk。

错误:“{classname} 类型的方法 {methodname} 必须覆盖超类方法”

相同的代码在具有可比配置的 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; }
}
4

8 回答 8

14

此功能仅在 Java 6 及更高版本中有效。我看到你使用的是jdk 1.6。那挺好的。可能的原因:您正在使用-source 1.5. 是这样吗?如果是这样,您可以将其更改为-source 1.6吗?

于 2011-02-14T19:11:20.387 回答
4

我也面临问题,刚刚解决。在Project->右键单击->properties->Java编译器中将“Compiler compliance level”更改为1.6。

于 2013-10-07T02:44:49.760 回答
3

在 eslipse 中可以使用不同版本的编译器。

请参阅 eclipse Preference->Java->Compiler “Compiler compliance level”中的配置。您必须选择“1.6”。

于 2011-02-14T20:10:52.993 回答
3

我刚刚发现,在使用 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>
于 2012-05-29T17:51:57.080 回答
1

所以,我一直遇到这个问题,在运行了几次上述解决方案之后,看到我的 Java 版本一直设置为 1.7,没有使用 Maven,所以只是退出并重新启动 Eclipse 几次@ jdowdell 建议,在此之后,它似乎可以工作,直到我下一次实施其中一个。我意识到当我重新启动 Eclipse 时,它​​提示我保存我的文件,我意识到我的接口没有被保存,所以原来的方法,被覆盖的方法,在磁盘上不存在。这就是为什么退出并重新启动 Eclipse 可以解决问题。

tl;dr :检查所有文件是否已保存。

于 2012-11-10T20:32:47.240 回答
0

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. Changing the jre back from jdk to the jre that Eclipse custom installed (or somebody, wasn't me).
  2. Disabling project specific settings in the compiler section, and also clicking Reset to Defaults, on both project and workspace settings for compiler.
  3. 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).

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

于 2012-06-02T02:09:38.813 回答
0

听起来您的方法签名不匹配。它们必须相同,包括诸如抛出的检查异常之类的东西。发布您的代码!

于 2011-02-14T18:48:53.087 回答
0

检查运行时库,编译器设置可能不同

右键单击项目名称,属性,Java Build Path,Libraries

查看 JRE 系统库的版本,很可能它设置为 1.5 或更低。选择 JRE System library,然后单击 remove 单击 Add Library,选择 JRE System Library,Next 然后 Workspace Default 或

于 2012-03-14T15:14:04.287 回答