首页 > 解决方案 > 编译器没有识别出我已经从它实现的接口实现了 compareTo() 方法

问题描述

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

在上面所有类/接口中声明的助手类中实现 compareTo 方法后,编译器会给出此错误。

Assistant.java:1:错误:助手不是抽象的,并且不会覆盖抽象学院公共类中的抽象方法 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

标签: javainterfaceabstract-classcomparable

解决方案


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

乍一看,带参数的版本似乎是您想要的版本,并且应该考虑到您的基类实现的事实Comparable<AbstractAffiliate>,因此只需完全删除您的抽象compareTo()方法就可以了。


推荐阅读