编译器没有认识到我已经从它实现的接口实现了compareTo()方法

问题描述 投票:-4回答:3

我正在通过抽象类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
java interface abstract-class comparable
3个回答
1
投票

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

乍一看,看起来像参数的版本是你想要的那个,并且应该由你的基类实现Comparable<AbstractAffiliate>的事实来处理,所以只需完全删除你的抽象compareTo()方法,你应该没问题。


0
投票

Comparable#compareTo的方法签名如下:

public int compareTo(T o);

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

public abstract int compareTo();

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


0
投票

这是因为你声明要实现Comparable<AbstractAffiliate>并且你没有实现该方法

int compareTo(AbstractAffiliate o)

你有一个不同的签名(没有参数)由于AbstractAffiliateAbstractFaculty被声明为摘要,他们不必从Comparable接口实现该方法。助理班需要。

请参阅Comparable

© www.soinside.com 2019 - 2024. All rights reserved.