本文介绍了Java Eclipse @Override错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个实现名为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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:54