我試圖做一個有理數的類,並重寫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;
}
就這樣做了。謝謝你發現 –