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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值