簡體   English   中英

編譯器沒有認識到我已經從它實現的接口中實現了 compareTo() 方法

[英]Compiler not Recognizing that I've implemented the compareTo() method from the Interface it Implements

我正在通過抽象類 AbstractAffiliate 實現一個接口“Comparable”,它由抽象類 Abstract Faculty 擴展,它由常規類 Assistant 擴展。

在輔助類中實現 compareTo 方法時,該方法已在上述所有類/接口中聲明,編譯器給出此錯誤。

Assistant.java:1: 錯誤:Assistant 不是抽象的,並且不會覆蓋 Abstract Faculty 公共類 Assistant 中的抽象方法 compareTo() 擴展 AbstractFaculty{ ^ 1 錯誤

我嘗試將泛化器添加到實現 Comparable 中。

抽象會員

public abstract class AbstractAffiliate implements Printable, Comparable<AbstractAffiliate>{

protected String name;
protected int age;
protected String address;
protected int phoneNumber;
protected int yearTheyCameToChapman;

    /**
    * Default empty AbstractAffiliate constructor
    */
public AbstractAffiliate() {
    super();
  age = 0;
  address = " ";
  phoneNumber = 0;
  yearTheyCameToChapman = 0;
}

public AbstractAffiliate(String name, int age, String address, int phoneNumber, int yearTheyCameToChapman){
    this.name = name;
    this.age = age;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.yearTheyCameToChapman = yearTheyCameToChapman;
}

public abstract String print();

public abstract int compareTo();

}

抽象教師

public abstract class AbstractFaculty extends AbstractAffiliate{

protected int facultyId;
protected String department;
protected int yearlySalary;
protected int numberOfPapers;

    /**
    * Default empty AbstractFaculty constructor
    */
public AbstractFaculty() {
    super();
  facultyId = 0;
  department = " ";
  yearlySalary = 0;
  numberOfPapers = 0;
}

    /**
    * Default AbstractFaculty constructor
    */
public AbstractFaculty(String name, int age, String address, int phoneNumber, int yearTheyCameToChapman, int facultyId, String department, int yearlySalary, int numberOfPapers) {
    super(name, age, address, phoneNumber, yearTheyCameToChapman);
    this.facultyId = facultyId;
    this.department = department;
    this.yearlySalary = yearlySalary;
    this.numberOfPapers = numberOfPapers;
    }

public abstract String print();

public abstract int compareTo();



}

助手

public class Assistant extends AbstractFaculty{

private int yearsUntilTenure;

public Assistant(){
  super();
  yearsUntilTenure = 0;
}

public Assistant(String name, int age, String address, int phoneNumber, int yearTheyCameToChapman, int facultyId, String department, int yearlySalary, int numberOfPapers, int yearsUntilTenure){
  super(name, age, address, phoneNumber, yearTheyCameToChapman, facultyId, department, yearlySalary, numberOfPapers);
  this.yearsUntilTenure = yearsUntilTenure;
}

public String print(){
  return "yup";
}


public int compareTo(AbstractAffiliate affiliate){
  if (this.yearTheyCameToChapman < affiliate.yearTheyCameToChapman){
    return 1;
  }
  if (this.yearTheyCameToChapman > affiliate.yearTheyCameToChapman){
    return -1;
  }
  else{
    return 0;
  }
}



}
```[enter image description here][1]


  [1]: https://i.stack.imgur.com/Xdz2F.png

你還沒有實現抽象方法。 抽象的.compareTo()方法沒有參數。 相比之下,您在Assistant類中實現的版本將AbstractAffiliate作為參數。 由於它們具有不同的參數,這使得它們完全不同的方法。

乍一看,采用參數的版本似乎是您想要的版本,這應該由您的基類實現Comparable<AbstractAffiliate>的事實來處理,因此只需完全刪除您的抽象compareTo()方法並你應該沒事。

這是因為您已經聲明要實現Comparable<AbstractAffiliate>而您沒有實現該方法

int compareTo(AbstractAffiliate o)

你有一個不同的簽名(無參數)因為AbstractAffiliateAbstractFaculty被聲明為抽象,他們不必從Comparable接口實現方法。 助理班需要。

參考比較

Comparable#compareTo 的方法簽名如下:

public int compareTo(T o);

但是在 Assistant 及其父類中,您添加了一個抽象方法

public abstract int compareTo();

這與Comparable#compareTo的方法不同。 只需刪除public abstract int compareTo(); 其父類中的方法將解決問題,無需一遍又一遍地聲明它。

暫無
暫無

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

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