eclipse 该方法必须覆盖或实现超类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14008918/
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
The method must override or implement a superclass method
提问by Shandan Spencer
Quick question. I am using Eclipse and I am getting the The method must override or implement a superclass method error, except Eclipse is using compliance of Java 1.7.
Here is my Code:
快速提问。我正在使用 Eclipse 并且我收到方法必须覆盖或实现超类方法错误,但 Eclipse 正在使用 Java 1.7 的合规性。
这是我的代码:
public abstract class M4 implements Armory {
@Override
public Integer weaponAmmo(int wepAmmo) {
wepAmmo = 10;
return wepAmmo;
}
@Override
public Integer weaponDamage(int wepDamage) {
wepDamage = 2;
return wepDamage;
}
@Override
public String weaponName(String wepName) {
wepName = "M4";
return wepName;
}
And here is the interface Code:
这是接口代码:
public interface Armory {
public Integer weaponAmmo(int wepAmmo);
public Integer weaponDamage(int wepDamage);
public String weaponName(String wepName);
}
Any Ideas?
有任何想法吗?
回答by Yogendra Singh
You don't need to use @override
annotation in your method implementation as you are not overriding the methods. You are just implementing the interface methods. This annotation is required when your override any super class methods.
您不需要@override
在方法实现中使用注释,因为您没有覆盖这些方法。您只是在实现接口方法。当您覆盖任何超类方法时,需要此注释。
Remove the @Override
annotations and it should be fine.
删除@Override
注释,它应该没问题。
回答by Evgeniy Dorofeev
In Java 5 @Override
was allowed only for methods overriding super class methods. Since Java 6 @Override
is also allowed for methods implementing interface methods.
在 Java 5 中@Override
,只允许覆盖超类方法的方法。由于 Java 6@Override
也允许用于实现接口方法的方法。
回答by Subin Sebastian
You are not overriding anything, you are implementing them . So i guess @Override can be removed.
你没有覆盖任何东西,你正在实施它们。所以我猜@Override 可以删除。
EDIT: I think your compiler is set to Java 1.5 in eclipse
编辑:我认为您的编译器在 Eclipse 中设置为 Java 1.5
Why does Eclipse complain about @Override on interface methods?
为什么 Eclipse 会在接口方法上抱怨 @Override?
@override on interface implementations are allowed from java 1.6 onwards.
从 java 1.6 开始,允许在接口实现上使用 @override。
回答by Ankit Shah
Even if Eclipse has been set to compliance level higher than Java 1.5 this error may occur because of Project properties as in your case. Right click on your Project > Properties > Java Compiler. Change it to 1.6 or higher here too. Do Project > Clean.
即使 Eclipse 已设置为高于 Java 1.5 的合规性级别,也可能由于项目属性而发生此错误,就像您的情况一样。右键单击您的项目 > 属性 > Java 编译器。在这里也将其更改为 1.6 或更高版本。做项目>清理。