问题描述
我正在编写一个实现名为Command的接口的Java类,它包含方法 isValid()
和 run()
,如下所示: public class DailyEnergy implements Command {
@Override
public boolean isValid(String command){
return false;
}
@Override
public void run(String command)throws异常{
}
}
,这里是Command.java文件:
public interface Command {
public boolean isValid(String command);
public void run(String command)throws异常;
}
在这个类中,我正在实现超类方法 isValid()
和 run()
,我想添加 @Override
注释,但是Eclipse提供了一个错误,指出这些方法必须覆盖超类方法。
即使我拿出方法并使用Eclipse自动导入,如果我添加注释,我得到错误。如果有人可以看出为什么我不能使用 @Override
注释,那将是非常感激的。
Java-6支持界面实现上的 @Override
注释。你可能在Java-5上吗? Oracle 在。它已经。见下面的例子:
I'm writing a Java class that's implementing an interface called Command, which contains the methods isValid()
and run()
, as follows:
public class DailyEnergy implements Command {
@Override
public boolean isValid(String command) {
return false;
}
@Override
public void run(String command) throws Exception {
}
}
and here's the Command.java file:
public interface Command {
public boolean isValid(String command);
public void run(String command) throws Exception;
}
Within this class, I'm implementing the superclass methods isValid()
and run()
, and I want to add the @Override
annotation, but Eclipse gives an error saying that "the methods must override superclass methods".
Even when I take out the methods and import them automatically with Eclipse, if I add the annotation, I get the error. If anyone can shed some light as to why I can't use the @Override
annotation, that would be greatly appreciated.
The @Override
annotation on interface implementations is supported since Java-6. Are you possibly on Java-5? Oracle has acknowledged a mess-up in the Java 6 docs. It has been corrected in Java-7. See example below:
这篇关于Java Eclipse @Override错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!