为什么我们必须在Java中重写equals()方法?
2022-09-02 11:09:55
我对我们覆盖该方法的原因有些困惑。.equals
例如:
Test test1 = new Test(3);
Test test2 = new Test(3);
//The if comparison does the same thing that the overridden `.equals()` method does.
if(test1.equals(test2)){
System.out.println("test1 and test2 are true in .equals()");
}
// Override .equals method.
public boolean equals(Object object) {
if(object instanceof Test && ((Test)object).getValue() == this.t) {
return true;
} else {
return false;
}
}
我不明白为什么我们必须重写该方法。.equals()