java中equals的重写_如何在java中重写equals方法

问题

我试图在java中重写equals方法。我有一个类人,基本上有2个数据字段名称和年龄。现在我想重写equals方法,以便我可以检查2个People对象。

我的代码如下

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方法只能比较String和age是Integer。

请帮我解决这个问题。

谢谢是进步。

我按建议使用了==运算符,我的问题解决了。非常感谢大家努力帮助我。

#1 热门回答(104 赞)

//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 people = new ArrayList();

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 (!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;

}

}

输出:>运行: - 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(总时间:0秒)

#2 热门回答(20 赞)

首先:你不是压倒equals,你是重载it。

如果没有看到age的实际声明,很难说你为什么会得到这个错误。

#3 热门回答(17 赞)

我不确定细节,因为你没有发布整个代码,但是:

记得也要覆盖hashCode()

equals方法应该有Object,而不是People作为它的参数类型。目前你正在超载,而不是覆盖,equals方法,这可能不是你想要的,特别是考虑到你以后检查它的类型。

你可以使用instanceof来检查它是否为People对象,例如if(!(其他instanceof People))

equals用于所有对象,但不用于基元。我认为你的意思是年龄是一个int(原始),在这种情况下只需使用==。注意,Integer(大写'I')是一个应与equals进行比较的Object。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值