为了账号安全,请及时绑定邮箱和手机立即绑定

重写了equals方法和hashcode方法后为什么equals方法显示true,但是hashcode方法显示他们的值却不一样?

重写了equals方法和hashcode方法后为什么equals方法显示true,但是hashcode方法显示他们的值却不一样?

qq_时光流逝而去_1 2017-03-18 18:19:22
public boolean equals(ComplexNumber other){ if(other==null) return false; if(other instanceof ComplexNumber){ if(this.realPart==other.realPart && this.imaginaryPart==other.imaginaryPart) return true; else return false; }else{ System.out.println("两个对象类型不一样无法比较"); return false; } } public int hashcode(){ //如果两个对象的值是相等的,那么他们的hash码也必须相同 //两个值相等的对象通过toString()方法得到的输出字符串是一样的 //于是,可以把得到的字符串的hash码当做对象的hash码 return this.toString().hashCode(); }控制台显示为:c1.equals(c2)=truec1.hashCode=2018699554c2.hashCode=1311053135
查看完整描述

1 回答

  • 1 回答
  • 1 关注
  • 1335 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信