如何覆盖Java中的equals方法 [英] How to override equals method in Java
问题描述
我正在尝试覆盖 Java 中的 equals 方法.我有一个 People
类,它基本上有 2 个数据字段 name
和 age
.现在我想覆盖 equals
方法,以便我可以在 2 个 People 对象之间进行检查.
I am trying to override equals method in Java. I have a class People
which basically has 2 data fields name
and age
. Now I want to override equals
method so that I can check between 2 People objects.
我的代码如下
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
但是当我编写 age.equals(other.age)
时,它给了我错误,因为 equals 方法只能比较字符串,而年龄是整数.
But when I write age.equals(other.age)
it gives me error as equals method can only compare String and age is Integer.
我按照建议使用了 ==
运算符,我的问题解决了.
I used ==
operator as suggested and my problem is solved.
推荐答案
//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++) {
boolean check = people.get(i).equals(people.get(y));
System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName());
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 (obj.getClass() != this.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;
}
}
输出:
运行:
-- Subash Adhikari - VS - K 错误
-- Subash Adhikari - VS - K false
-- Subash Adhikari - VS - StackOverflow false
-- Subash Adhikari - VS - StackOverflow false
-- Subash Adhikari - VS - Subash Adhikari 真
-- Subash Adhikari - VS - Subash Adhikari true
-- K - VS - StackOverflow 错误
-- K - VS - StackOverflow false
-- K - VS - Subash Adhikari 错误
-- K - VS - Subash Adhikari false
-- StackOverflow - VS - Subash Adhikari 错误
-- StackOverflow - VS - Subash Adhikari false
-- 构建成功(总时间:0 秒)
-- BUILD SUCCESSFUL (total time: 0 seconds)
这篇关于如何覆盖Java中的equals方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!