非抽象类不能覆盖Comparable中的抽象方法compareTo?

时间:2017-02-27 20:08:06

标签: java interface abstract comparable

我有一个类Vertex<T>,它实现IVertex<T>,实现Comparable。每当我编译我的代码时,我都会收到错误:

  

Vertex不是抽象的,不会覆盖抽象方法   compareTo(IVertex)in Comparable

这个问题是,我无法更改界面IVertex中的任何代码,因为这是我老师指示的内容。我该如何解决这个问题?我在下面提供了我的代码:

顶点:

 package student_solution;


import graph_entities.*;

import java.util.*;

public class Vertex<T> implements IVertex<T>{

  // Add an edge to this vertex.

  public void addEdge(IEdge<T> edge){

  } 

  // We get all the edges emanating from this vertex:  

    public Collection< IEdge<T> > getSuccessors(){

    }

    // See class Label for an an explanation:

    public Label<T> getLabel(){

    }

    public void setLabel(Label<T> label){

    }

  }

IVertex:

package graph_entities;

import java.util.Collection;

public interface IVertex<T> extends Comparable<IVertex<T>>
{

  // Add an edge to this vertex.

  public void addEdge(IEdge<T> edge);

  // We get all the edges emanating from this vertex:  

public Collection< IEdge<T> > getSuccessors();

  // See class Label for an an explanation:

public Label<T> getLabel();

public void setLabel(Label<T> label);

}

提前谢谢!

1 个答案:

答案 0 :(得分:2)

正如错误所示,您的类实现了interface扩展Comparable。现在,为了使您的课程具体化,您必须override您的班级正在实施interfaces的所有方法。

因此,在您的情况下,您需要做的就是覆盖顶点compareTo中的class方法,例如:

@Override
public int compareTo(IVertex<T> o) {
    // implementation
    return 0;
}

Here关于接口和继承的Oracle文档。