簡體   English   中英

Java,如何從抽象類繼承方法

[英]Java, how to inherit methods from abstract class

我有一個抽象類Person和接口可比,它也用於程序的其他部分。 目前我在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 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;
    }
}

您的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方法),那么它將是編譯器錯誤。

您的Comparable接口有一個方法compareTo(Element nodeA, Element nodeB) 此方法未在Student中定義,也未在Person中定義。 人有以下方法:

public int compareTo(Person personB)

,它不會覆蓋compareTo(Person nodeA, Person nodeB)

為什么要重新定義標准的java.util.Comparable接口?

您應該實現接口中出現的方法,即使用兩個參數

public int compareTo(Person nodeA, Person nodeB)

為避免將來出現此類問題,請使用@Override注釋:

@Override
public int compareTo(Person nodeA, Person nodeB)

如果您嘗試覆蓋方法,但在簽名中出錯,則會導致編譯錯誤。

另外,請考慮使用Java的標准Comparable

更改您的界面:

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

至:

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

並將您的Person類定義為:

abstract class Person implements Comparable<? extends Person> { /* ... */ }

並讓你的學生(和其他人 - 子類):

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

就這些。

你需要實施

public int compareTo(Person nodeA, Person nodeB)

在你的Person類中。 目前你只有:

public int compareTo(Person personB)

您需要在Student類中創建compareTo方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM