美文网首页技术文
Effective Java 第三章笔记

Effective Java 第三章笔记

作者: ktdroid | 来源:发表于2016-07-24 16:02 被阅读0次

对于所有对象都通用的方法

1. 覆盖 equals 时请遵守通用约定

  • 自反性
x != null
x.equals(x) == true
  • 对称性
x != null && y != null
when x.equals(y) == true then y.equals(x) == true
  • 传递性
x != null && y != null && z != null
when x.equals(y) == true && y.equals(z) == true then x.equals(z) == true
  • 一致性
  • 非空性
x != null
x.equals(null) == false

2. 覆盖 equals 时总要覆盖 hashCode

3. 始终要覆盖 toString

4. 谨慎地覆盖 clone

5. 考虑实现 Comparable 接口

相关文章

网友评论

    本文标题:Effective Java 第三章笔记

    本文链接:https://www.haomeiwen.com/subject/oezwjttx.html