【问题标题】:Compiler not Recognizing that I've implemented the compareTo() method from the Interface it Implements编译器没有识别出我已经从它实现的接口实现了 compareTo() 方法
【发布时间】:2019-09-11 22:22:22
【问题描述】:

我正在通过抽象类 AbstractAffiliate 实现一个接口“Comparable”,该接口由抽象类 Abstract Faculty 扩展,该抽象类由常规类 Assistant 扩展。

在助手类中实现 compareTo 方法(已在上述所有类/接口中声明)时,编译器会给出此错误。

Assistant.java:1: 错误:Assistant 不是抽象的,并且不会覆盖 Abstract 中的抽象方法 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

【问题讨论】:

  • public abstract int compareTo(); 没有参数,你会将它与什么进行比较?
  • 也许我遗漏了一些东西,但是你在抽象类中声明了public abstract int compareTo();,但你实现了public int compareTo(AbstractAffiliate affiliate){...,所以你仍然需要实现compareTo();(或从抽象类中删除它)。

标签: java interface abstract-class comparable


【解决方案1】:

您还没有实现抽象方法。抽象的.compareTo() 方法不带参数。相比之下,您在Assistant 类中实现的版本采用AbstractAffiliate 作为参数。由于它们具有不同的参数,这使得它们完全不同的方法。

乍一看,带参数的版本似乎是您想要的版本,应该注意您的基类实现了Comparable&lt;AbstractAffiliate&gt;,因此只需删除您的抽象compareTo()方法完全没问题。

【讨论】:

  • 不过话说回来,这个抽象的 compareTo 方法是在 Comparable 接口中定义的,所以不需要再次声明。
  • @AndyTurner 不错。我没有将代码向右滚动到足以注意到这一点。愚蠢的笔记本电脑屏幕。
  • 借口:我在打电话:)
【解决方案2】:

这是因为你已经声明实现Comparable&lt;AbstractAffiliate&gt;,而你没有实现该方法

int compareTo(AbstractAffiliate o)

您的签名不同(无参数) 由于AbstractAffiliateAbstractFaculty 被声明为抽象,它们不必实现来自Comparable 接口的方法。助理类需要。

参考Comparable

【讨论】:

    【解决方案3】:

    Comparable#compareTo 的方法签名如下:

    public int compareTo(T o);
    

    但是在 Assistant 和它的父类中你添加了一个抽象方法

    public abstract int compareTo();
    

    这与Comparable#compareTo的方法不同。只需从其父类中删除public abstract int compareTo(); 方法即可解决问题,无需一次又一次地声明它。

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 2018-10-04
      • 1970-01-01
      • 2011-09-15
      • 2018-10-24
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多