Java,如何从抽象类继承方法

3
我有一个抽象类Person和一个接口comparable,这也被用于程序的其他部分。当前在Person中有一个名为compareTo()的方法。当我尝试编译时,出现以下错误提示:
The type Student must implement the inherited abstract method 
 Comparable<Person>.compareTo(Person, Person)

我需要做什么?我不想在任何子类中实现此方法,因为我需要所有子类(学生、导师、教授等)都使用此方法。有更好的方法吗?

接口:

interface Comparable<Element> {
    public int compareTo(Element nodeA, Element nodeB);
}

Abstract class Person:

abstract class Person implements Comparable<Person> {

    protected String name;

    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String newName) {
        name = newName;
    }
    public String toString() {
        return name;
    }

    public int compareTo(Person personB) {
        int comp = this.name.compareTo(personB.getName());
        return comp;
    }
}

定义学生类

class Student extends Person implements Comparable<Person> {

    private int id;

    public Student(String name, int id) {
        super(name);
        this.id = id;
    }

    public int getID() {
        return id;
    }

    public void setID(int newID) {
        id = newID;
    }

    public String toString() {
        return id + ", " + name;
    }
}
7个回答

2
您的Comparable<Element>类声明了一个方法public int compareTo(Element nodeA, Element nodeB);,但在您的Person类中,您实现了public int compareTo(Person personB),这不是相同的方法签名。
您需要实现public int compareTo(Person personA, Person personB),或者修改您的Comparable<Element>类的方法定义为public int compareTo(Element other);以覆盖核心Comparable类的compareTo方法
此外,正如@murat在下面的评论中提到的那样,使用@Override注释会有所帮助(假设您使用的是Java版本1.5或更高版本)。如果您将@Override添加到您实际上没有从超类覆盖的方法中(例如您的两个参数的compareTo方法),那么它将是一个编译器错误。

2
这可能是最完整正确的答案。此外,最好在覆盖方法之前添加 @Override 标记,以确保它们与超类或接口中的某些方法匹配。 - murat
用户Hound Dog的评论(没有评论权限):如果您使用像Eclipse这样的IDE,您可以轻松地从接口或抽象类中生成方法(在Eclipse中使用Source->Override/Implement Methods),并防止定义没有相同签名的新方法。 - Anne

2
您的Comparable接口有一个方法compareTo(Element nodeA, Element nodeB)。这个方法在Student中没有定义,在Person中也没有定义。Person有以下方法:
public int compareTo(Person personB)

为什么要重新定义标准的java.util.Comparable接口?

compareTo(Person nodeA, Person nodeB)不会覆盖它。


2
你应该按照接口中的方法实现,即使用两个参数。
public int compareTo(Person nodeA, Person nodeB)

为了避免将来出现这样的问题,请使用@Override注解:
@Override
public int compareTo(Person nodeA, Person nodeB)

这会导致编译错误,如果您尝试覆盖一个方法,但在其签名中出现错误。

此外,请考虑使用Java的标准Comparable

2

将您的界面从以下形式更改:

interface Comparable<Element> 
{     
   public int compareTo(Element nodeA, Element nodeB); 
}

to:

interface Comparable<Element> 
{     
   public int compareTo(Element nodeA); 
}

并且将你的Person类定义为:
abstract class Person implements Comparable<? extends Person> { /* ... */ }

让你的学生(以及其他人物子类)成为:

class Student extends Person { /* ... */ }

这就是全部。


0
你需要在Student类中创建一个compareTo方法。

0
你需要实现。
public int compareTo(Person nodeA, Person nodeB)

在你的Person类中,目前你只有:
public int compareTo(Person personB)

他正在定义自己的Comparable接口,请参见问题中的代码。 - Abdullah Jibaly
抱歉,我已相应地删除了我的评论。 - Vilas

0
interface Comparable<Element> {
    public int compareTo(Element nodeA, Element nodeB);
}

如果是以下情况之一,那就有意义:

interface Comparator<Element> { //comparator compares two instances of same type
    public int compare(Element nodeA, Element nodeB);
}

或者(对于您的情况必须是这样的):

interface Comparable<Element> { //comparable compares itself with another instance of same type
    public int compareTo(Element that);
}

但对于这两种情况,您都应该使用标准的Java接口:即使您只使用Comparable,也请参阅Comparator


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接