我试图做一个有理数的类,并重写equals和hash代码的方法。但是,我的平等在这种情况下会回归真实,即它的分子和分母是不同的。任何想法可能会造成这种情况?覆盖等于方法问题
public boolean equals(Object rhs) {
if (this == rhs){
return true;
}
if (rhs == null){
return false;
}
if (!(rhs instanceof Rational)){
return false;
}
Rational other = (Rational) rhs;
if (denom == other.denom){
if (num == other.num);{
return true;
}
}
return false;
}
就这样做了。谢谢你发现 –