If one is writing a class that is going to have its objects be compared in some way, then one should override the equals
and hashCode
methods.
Not providing an explicit equals
method will result in inheriting the behavior of the equals
method from the superclass, and in the case of the superclass being the Object
class, then it will be the behavior setforth in the Java API Specification for the Object
class.
The general contract for providing an equals
method can be found in the documentation for the Object
class, specifically, the documentation of the equals
and hashCode
methods.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…