2017-02-27 189 views
1

我有一个类Vertex<T>其实施IVertex<T>其实施Comparable。每当我编译我的代码,我得到的错误:非抽象类不能覆盖Comparable中的抽象方法compareTo?

Vertex is not abstract and does not override abstract method 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); 

} 

预先感谢您!

+2

那么,实现你忘了实现的方法(即'public int compareTo(IVertex v)'。 –

+0

编译器告诉你你的类必须实现compareTo方法 - 就这么简单吧。 – duffymo

+0

更多阅读错误信息它说的是顶点,而不是'IVertex'。 – EJP

回答

2

由于错误提示,您的班级实施了interface,它延伸了Comparable。现在,为了让您的课具体化,您必须override您班正在实施的interfaces的所有方法。

所以,你的情况,你需要做的是覆盖顶点classcompareTo方法,例如:

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

Here的甲骨文上的接口和继承文档。

+0

大部分都是,但他需要实现的方法不是'compareTo(T)'而是'compareTo(IVertex )',这可能会变成一个设计问题,这取决于比较应该如何实现 –

+0

@JohnBollinger谢谢指出它我错过了:(是的,它可能会成为一个设计问题,但由于OP被指示不改变'interface',我们不能做太多。 –

+0

谢谢!我想我在看到c时感到恐慌ommandline错误。初学者错误。 – assassinweed2

相关问题