我實現了compareTo()方法我有以下類實現了Comparable
接口。我已經在其中定義了compareTo()
方法,但不知何故編譯器仍然告訴我必須實現它。編譯器告訴我,當我已經有
public class Person implements Comparable {
private String fName;
private String lName;
private Integer age;
public Person (String fName, String lName, int age)
{
this.fName = fName;
this.lName = lName;
this.age = age;
}
// Compare ages, if ages match then compare last names
public int compareTo(Person o) {
int thisCmp = age.compareTo(o.age);
return (thisCmp != 0 ? thisCmp : lName.compareTo(o.Name));
}
}
錯誤消息:
The type Person must implement the inherited abstract method Comparable.compareTo(Object)
Syntax error on token "=", delete this token
at me.myname.mypkg.Person.<init>(Person.java:6)
我敢肯定的,我沒有投給根類Object
在compareTo()
方法。那麼,我可能做錯了什麼?
非常感謝。我可以問,如果我將內部變量聲明爲一個變量,那麼是否有可能擁有自己的compareTo()方法? – kype
這會使執行過程變得複雜...... – Reimeus
好的,謝謝你,會按照原文 – kype