我试图实施comparable
以便使用,但由于某种原因,它没有在超类中找到compareTo
方法。我正在比较字符串供参考。我不明白为什么我的“Comparable”接口不工作
编辑:对不起,我忘了添加我收到的错误。那就是:
Employee.java:32: error: cannot find symbol
return super.compareTo((Object)other);
^
symbol: method compareTo(Object)
代码:
public abstract class Employee implements Cloneable, Comparable
{
private Name name;
private double weeklyPay;
public Employee(String first, String middle, String last, double weeklyPay)
{
this.weeklyPay = weeklyPay;
name = new Name(first,middle,last);
}
public Employee(String last, double weeklyPay)
{
this.name = name;
this.weeklyPay = weeklyPay;
name = new Name(last);
}
public Employee(String first, String last, double weeklyPay)
{
this.name = name;
this.weeklyPay = weeklyPay;
name = new Name(first,last);
}
public abstract double getWeeklyPay();
public String getFullName()
{
return name.getFullName();
}
public int compareTo(Object other)
{
return super.compareTo((Object)other);
}
public int compareTo(Name name)
{
return compareTo((Object)name.getFullName());
}
有什么不合适呢?请提供更多详情,以便我们能够帮助您。 – 2013-04-07 18:51:14
什么是超类比类? – 2013-04-07 18:52:03
如果Employee不扩展Object以外的任何类,则不能调用super.compareTo,becase super指的是Object类,Object中没有compareTo方法。 – 2013-04-07 18:54:01