简体   繁体   English

非抽象类不能重写Comparable中的抽象方法compareTo吗?

[英]Non abstract class cannot override abstract method compareTo in Comparable?

I have a class Vertex<T> which implements IVertex<T> which implements Comparable . 我有一个Vertex<T>类,该类实现IVertex<T>IVertex<T>实现Comparable Whenever, I compile my code, I get the error: 每当我编译代码时,都会出现错误:

Vertex is not abstract and does not override abstract method compareTo(IVertex) in Comparable 顶点不是抽象的,也不覆盖Comparable中的抽象方法compareTo(IVertex)

The issue with this is that, I CANNOT change any of the code within the interface IVertex as that is what my teacher has instructed. 问题是,我不能更改接口IVertex内的任何代码,因为这是我的老师所指示的。 How can I solve this issue? 我该如何解决这个问题? I have included my code below: 我在下面包含了我的代码:

Vertex: 顶点:

 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: 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);

}

Thank you in advance! 先感谢您!

As the error suggests, your class implements an interface which extends Comparable . 如错误所示,您的类实现了扩展Comparableinterface Now, in order to make your class concrete, you have to override all the methods of interfaces your class is implementing. 现在,为了使您的类具体化,您必须override类正在实现的所有interfaces方法。

So, in your case, all you need to do is to override compareTo method in Vertex class , eg: 因此,根据您的情况,您需要做的就是重写Vertex class compareTo方法,例如:

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

Here 's Oracle's documentation on interfaces and inheritance. 是有关接口和继承的Oracle文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM