我沒有看到任何東西,我做錯了,但NetBeans的給了我以下錯誤:Java的可比接口compareTo方法
incomparable types
required: boolean
found: java.lang.Object
public int compareTo(Object obj) {
if(obj instaceof Employee){
Employee employee = (Employee) obj;
if(this.weekly_earnings > employee.weekly_earnings)
return 1;
else if(this.weekly_earnings == employee.weekly_earnings)
return 0;
else
return -1;
}
else{
System.out.println("Error");
}
}
instaceof不存在 – 2010-03-05 00:26:38
它不返回任何錯誤情況(應拋出'ClassCastException',實際上應該使用泛型)。 – 2010-03-05 00:29:10
我還想補充一點,如果您剛剛返回(this.weekly_earnings - employee.weekly_earnings),那麼可能會更好,保存所有if語句。 – DaveJohnston 2010-03-05 00:32:27