Effective Java 英文 第二版 读书笔记 Item 8:Obey the general contract when overriding equals

title:重载equals方法要遵循的一般约定

Each instance of the class is inherently unique 实例存在主键

You don't care whether the class provides a "logical equality" test.

so the equals implementation inherited form Object is adequate.

使用场景

when a class has a notion of logical equality that differs form mere object identity,

and a superclass has not already overridden equals to implement the desired behavior

基本原则

use the == operator to check if the argument is a reference to this object

use the instanceof operator to check if the argument has the corrcet type

cast the argument to correct type.

for each "significant" field in the class,check if that field of the argument matched the corresponding field of this object.

Always override hashCode when you override equals (Item 9)

原文地址:https://www.cnblogs.com/linkarl/p/5532244.html