编译器无法识别我已经从其实现的接口实现了compareTo()方法

时间:2019-04-22 18:30:29

标签: java interface abstract-class comparable

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

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

Assistant.java:1:错误:助手不是抽象的,并且不覆盖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

3 个答案:

答案 0 :(得分:1)

您尚未实现abstract方法。抽象的.compareTo()方法不带参数。相比之下,您在Assistant类中实现的版本将AbstractAffiliate作为参数。由于它们具有不同的参数,因此使其成为完全不同的方法。

乍一看,带有参数的版本看起来像是您想要的版本,应该通过基类实现Comparable<AbstractAffiliate>的事实来加以解决,因此只需删除抽象{{ 1}}方法完全可以,您应该没事。

答案 1 :(得分:0)

这是因为您已声明要实现col1|col2|prop1|prop2|prop3 --------------------------- 1 |1 |A |B |C ,而不是要实现该方法

Comparable<AbstractAffiliate>

您的签名不同(无参数) 由于int compareTo(AbstractAffiliate o) AbstractAffiliate被声明为抽象,因此它们不必从AbstractFaculty接口实现该方法。助理班需要。

请参阅Comparable

答案 2 :(得分:0)

Comparable#compareTo的方法签名如下:

public int compareTo(T o);

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

public abstract int compareTo();

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