我試圖實施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