我有一个抽象类Person和接口可比,它也用于程序的其他部分.目前我在Person中有一个方法compareTo().当我尝试编译时,我得到:
The type Student must implement the inherited abstract method
pareTo(Person, Person)
我到底要做什么?我不会在任何子类中实现此方法,因为我需要所有这些方法,学生,导师,教授等…有更好的方法吗?
接口:
interface Comparable {
public int compareTo(Element nodeA, Element nodeB);
}
抽象类人物:
abstract class Person implements Comparable {
protected String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public String toString() {
return name;
}
public int compareTo(Person personB) {
int comp = pareTo(personB.getName());
return comp;
}
}
和班级学生
class Student extends Person implements Comparable {
private int id;
public Student(String name, int id) {
super(name);
this.id = id;
}
public int getID() {
return id;
}
public void setID(int newID) {
id = newID;
}
public String toString() {
return id + ", " + name;
}
}
解决方法:
更改您的界面:
interface Comparable
{
public int compareTo(Element nodeA, Element nodeB);
}
至:
interface Comparable
{
public int compareTo(Element nodeA);
}
并将您的Person类定义为:
abstract class Person implements Comparable extends Person> { /* ... */ }
并让你的学生(和其他人 – 子类):
class Student extends Person { /* ... */ }
就这些.
标签:java,inheritance,abstract,methods
来源: https://codeday.me/bug/0726/1540911.html