そのため、多くのカスタム クラスには、コンポジションを使用して内部にカスタム クラスも含まれています。
カスタム クラスには頻繁に変更される変数があり、それらを HashSets に追加します。私の質問は、hashCode を実装するときです。常に変化するプライベート フィールドしか持たないクラスに対してはどうすればよいでしょうか。
カスタム クラスの例を次に示します。
public class Cell {
protected boolean isActive;
protected boolean wasActive;
public Cell() {
this.isActive = false;
this.wasActive = false;
}
// getter and setter methods...
@Override
public int hashCode() {
// HOW SHOULD I IMPLEMENT THIS IF THIS custom object is constantly
// being added into HashSets and have it's private fields isActive
// and wasActive constantly changed.
}
// ANOTHER QUESTION Am I missing anything with this below equals implementation?
@Override
public boolean equals(Object object) {
boolean result = false;
if (object instanceof Cell) {
Cell otherCell = (Cell) object;
result = (this.isActive == otherCell.isActive && this.wasActive ==
otherCell.wasActive);
}
return result;
}