問題描述
我有一個現有項目在覆蓋 interface 方法而不是超類方法的方法上使用 @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:
- 打開窗口 > 首選項對話框
- 瀏覽到 Java > 編譯器.
- 在那里,將編譯器合規級別"設置為 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!