compareTo方法无法编译

Col*_*een 1 java interface compareto comparable

我的班级标题:

public class GraphEdge implements Comparable<GraphEdge>{

/** Node from which this edge starts*/
protected Point from;
/** Node to which this edge goes*/
protected Point to;
/** Label or cost for this edge*/
protected int cost;
Run Code Online (Sandbox Code Playgroud)

我的compareTo方法:

@Override
public int compareTo(GraphEdge other){
    return this.cost-other.cost;
}
Run Code Online (Sandbox Code Playgroud)

但是Eclipse给了我错误:

GraphEdge类型的compareTo(GraphEdge)方法必须覆盖超类方法

whyyyyy?我尝试过做Comparable,用

@Override
public int compareTo(Object o){
            GraphEdge other = (GraphEdge) o;
    return this.cost-other.cost;
}
Run Code Online (Sandbox Code Playgroud)

但这也失败了.

icy*_*com 7

很可能您的项目设置为Java 1.5合规级别 - 尝试将其设置为1.6,它应该可以工作.这里没有Eclipse进行测试,但是我记得当设置为1.5时我无法在接口上使用@Override(但可以在类上)方法覆盖.设置为1.6时,此功能正常.

即设置为1.5时应该失败,但在1.6时工作正常:

interface A {
   void a();
}

class B implements A {
   @Override
   public void a() {
   }
}
Run Code Online (Sandbox Code Playgroud)

试试吧:

在此输入图像描述