我在学生抽象类中有一个抽象方法。子类JUPASStudent覆盖它。现在,我正在尝试对Student类型的ArrayList进行排序。它需要调用该抽象方法,但我无法这样做。
我正在这里写我的部分代码。
超类:
abstract class Student {
protected String name;
public Student (String name) {
this.name = name;
}
public String getName() {
return name;
}
abstract double getResult(); //abstract method
}
子类:
public class JUPASStudent extends Student {
private double dseResult;
public JUPASStudent(String n, double d) {
super (n);
this.dseResult = d;
}
public double getResult() {
return dseResult; //abstract method is overridden here
}
public String getName() {
return super.name;
}
public void setResult(double dseResult) {
this.dseResult = dseResult;
}
public void setName(String name) {
super.name = name;
}
}
现在我正在编写排序函数以创建ArrayList(Student类)
public ArrayList <Student> sort(ArrayList<Student> sList){
sList.sort(Comparator.comparingInt(Student::getResult));
return sList;
}
输入:
A 9
B 5
C 8
预期输出:
B 5
C 8
A 9
错误
-The method comparingInt(ToIntFunction<? super T>) in the type Comparator is not applicable for the arguments (Student::getResult)
-The type Student does not define getResult(T) that is applicable here
如果您在子类中重写了该方法,它将使用子类的实现,因此,至少在您的情况下,不必担心显式调用它