如何在java中重写equals方法?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (49)

代码如下

public boolean equals(People other){
    boolean result;
    if((other == null) || (getClass() != other.getClass())){
        result = false;
    } // end if
    else{
        People otherPeople = (People)other;
        result = name.equals(other.name) &&  age.equals(other.age);
    } // end else

    return result;
} // end equals
提问于
用户回答回答于
//Written by K@stackoverflow
public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(new Person("Subash Adhikari",28));
    people.add(new Person("K",28));
    people.add(new Person("StackOverflow",4));
    people.add(new Person("Subash Adhikari",28));

    for (int i=0;i<people.size()-1;i++){
        for (int y=i+1;y<=people.size()-1;y++){
            System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName());
            boolean check = people.get(i).equals(people.get(y));
            System.out.println(check);
        }
    }
}
}

//written by K@stackoverflow
public class Person {
private String name;
private int age;

public Person(String name, int age){
    this.name = name;
    this.age = age;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (!Person.class.isAssignableFrom(obj.getClass())) {
        return false;
    }
    final Person other = (Person) obj;
    if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
        return false;
    }
    if (this.age != other.age) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
    hash = 53 * hash + this.age;
    return hash;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

run:

-- Subash Adhikari - VS - K false

-- Subash Adhikari - VS - StackOverflow false

-- Subash Adhikari - VS - Subash Adhikari true

-- K - VS - StackOverflow false

-- K - VS - Subash Adhikari false

-- StackOverflow - VS - Subash Adhikari false

-- BUILD SUCCESSFUL (total time: 0 seconds)

用户回答回答于

没有看到age很难说出你为什么会犯这个错误。

扫码关注云+社区

领取腾讯云代金券