当前位置: 动力学知识库 > 问答 > 编程问答 >

override - java: overridden compareTo() not recognized in static method

问题描述:

I wrote a class called Member that implements Comparable.

I have overridden the compareTo method

I wrote another class called BinarySearch that contains one static method called binarySearch

in that method i have:

 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;

}

}

The line:

 member.compareTo(list.get(middle))

gives an error saying: cannot find method compareTo(object).

Yet the compiler can find the method in my other classes.

All other methods of my Member class are visible in the binarySearch method as well.

the Member class:

 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;

}

}

}

网友答案:

Your List doesn't have a type parameter, so everything you get from it defaults to being treated as an Object, while judging by your error message your compareTo only accepts comparison with other Member objects. You can fix this by changing the type of the list from List to List<Member> - or alternatively (but kludgier) rework your compareTo to work with Object or use type casting.

分享给朋友:
您可能感兴趣的文章:
随机阅读: