为什么 Eclipse 会在接口方法上抱怨 @Override? [英] Why does Eclipse complain about @Override on interface methods?

查看:30
本文介绍了为什么 Eclipse 会在接口方法上抱怨 @Override?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有项目,它在覆盖接口方法而不是超类方法的方法上使用@Override.我无法在代码中更改这一点,但我希望 Eclpse 停止抱怨注释,因为我仍然可以使用 Maven 进行构建.

I have an existing project that uses @Override on methods that override interface methods, rather than superclass methods. I cannot alter this in code, but I would like Eclpse to stop complaining about the annotation, as I can still build with Maven.

我将如何禁用此错误?

注意:由于项目要求,我需要为Java 1.5编译.

推荐答案

在实现接口声明的方法的方法上使用 @Override 注释仅从 Java 6 开始有效.这是 Java 5 中的错误.

Using the @Override annotation on methods that implement those declared by an interface is only valid from Java 6 onward. It's an error in Java 5.

确保您的 IDE 项目设置为使用 Java 6 JRE,并且源代码兼容性"设置为 1.6 或更高:

Make sure that your IDE projects are setup to use a Java 6 JRE, and that the "source compatibility" is set to 1.6 or greater:

  1. 打开窗口">首选项"对话框
  2. 浏览到 Java > 编译器.
  3. 在那里,将编译器合规性级别"设置为 1.6.

请记住,Eclipse 可以覆盖特定项目的这些全局设置,因此也请检查这些设置.

Remember that Eclipse can override these global settings for a specific project, so check those too.

更新:

Java 5 下的错误不仅仅在于 Eclipse;直接从命令行使用 javac 会给你同样的错误.它不是有效的 Java 5 源代码.

The error under Java 5 isn't just with Eclipse; using javac directly from the command line will give you the same error. It is not valid Java 5 source code.

但是,您可以为 JDK 6 的 javac 指定 -target 1.5 选项,这将从 Java 6 源代码生成 Java 5 版本类文件.

However, you can specify the -target 1.5 option to JDK 6's javac, which will produce a Java 5 version class file from the Java 6 source code.

这篇关于为什么 Eclipse 会在接口方法上抱怨 @Override?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆