java:overridden compareTo()在静态方法中无法识别

时间:2014-05-04 07:31:25

标签: java override compareto

我写了一个名为Member的类来实现Comparable。 我已经覆盖了compareTo方法 我编写了另一个名为BinarySearch的类,它包含一个名为binarySearch的静态方法 在那个方法中我有:

    public class BinarySearch {


    private BinarySearch(){
    }

    public static int binarySearch(List<Member> list, Member member){
       int low = 0;                  
       int high = list.size();     
       int middle;                  

    while ( low <= high ) 
    {
      middle = ( low + high ) / 2;
      if ( member.equals(list.get(middle)))  
         return middle;
      else if ( member.compareTo(list.get(middle)) < 0)
         high = middle - 1;  
      else
         low = middle + 1;   
   }

   return -1; 
}

}

该行:

    member.compareTo(list.get(middle))

给出一个错误说:找不到方法compareTo(object)。 然而,编译器可以在我的其他类中找到该方法。 我的Member类的所有其他方法也可以在binarySearch方法中看到。

会员班:

    public class Member implements Comparable<Member>{
        private String firstName;
        private String lastName;
        private String email;
        private String phoneNumber;
        private int level;

    public Member(){
        this("","","","",-1);
    }

    public Member(String firstName, String lastName, String email, String phoneNumber,      int level){
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.level = level;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

public String getEmail() {
    return email;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public int getLevel() {
    return level;
}

public Member setFirstName(String firstName) {
    return new Member(firstName, this.lastName, this.email, this.phoneNumber, this.level);
}

public Member setLastName(String lastName) {
    return new Member(this.firstName, lastName, this.email, this.phoneNumber, this.level);
}

public Member setEmail(String email) {
    return new Member(this.firstName, this.lastName, email, this.phoneNumber, this.level);
}

public Member setPhoneNumber(String phoneNumber) {
    return new Member(this.firstName, this.lastName, this.email, phoneNumber, this.level);
}

public Member setLevel(int level) {
    return new Member(this.firstName, this.lastName, this.email, this.phoneNumber, level);
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Member other = (Member) obj;
    if (!Objects.equals(this.lastName, other.lastName)) {
        return false;
    }
    if (!Objects.equals(this.phoneNumber, other.phoneNumber)) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Member{" + "firstName=" + firstName + ", lastName=" + lastName + 
            ", email=" + email + ", phoneNumber=" + phoneNumber + ", level=" + 
            level + '}';
}

@Override
public int compareTo(Member t) {
    if (equals(t)){
        return 0;
    }
    if (lastName.compareToIgnoreCase(t.lastName) == -1){
        return -1;
    }else{
        return 1;
    }

}

}

1 个答案:

答案 0 :(得分:0)

您的列表没有类型参数,因此您get的所有内容都默认被视为Object,而根据您的错误消息判断您的compareTo仅接受比较与其他Member个对象。您可以通过将列表类型从List更改为List<Member>来解决此问题,或者替换(但kludgier)重新设置compareTo以使用Object或使用类型转换。