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)=true
c1.hashCode=2018699554
c2.hashCode=1311053135
相关分类