1 重写equals方法时请遵守通用约定

(1)无需覆盖equals方法的情况

  • 要求独一无二
  • 不要求逻辑相等
  • 超类已经覆盖equals方法,对其子类也适用
  • 一个类是私有的或者是包私有(可以重写后抛出异常,防止被重写)

(2)重写equals方法要保持等价关系

  • 自反性:对于任意非空引用值x,x.equals(x)必须返回true。
  • 对称性:对于任意非空引用值x和y,x.equals(y)必须返回true,当且仅当y.equals(x)返回true。
  • 传递性:对于任意非空引用值x,y,z,如果x.equals(y)返回true而且y.equals(z)也返回true,那么x.equals(z)必须返回true。
  • 一致性:对于任意非空引用值x和y,只要equals方法中使用的信息没有被修改,那么不管多少次调用x.equals(y)都必须一致性地返回true或者false。
  • 对于任意非空引用值x,x.equals(null)必须返回false。

注:重写equals方法需要打破等价关系是不被允许的。

2 重写equals方法都要重写hashCode方法

(1)equals与hashCode的关系

  • equals相等,hashCode必须相等。
  • equals不等,hashCode可以相等,也可以不等。
  • hashCode不等,equals一定不等。

(2)重写原则

  • 不要为了提高性能而试图在哈希码的计算过程中将重要的属性排除掉:防止大量hash冲突
  • 不要为hashCode返回的值提供详细的规范:为了未来拓展

3 始终重写toString方法

(1)重写原则

  • toString方法应该返回类里面所有有用的信息
  • 无论我们是否决定要指明格式,我们都应该清晰地在文档中表达我们的意图

(2)DEMO

  • 指定格式:程序员自行补充注释和格式
/**
* Returns the string representation of this phone number.
* The string consists of twelve characters whose format is
* "XXX-YYY-ZZZZ", where XXX is the area code, YYY is the
* prefix, and ZZZZ is the line number. Each of the capital
* letters represents a single decimal digit. *
* If any of the three parts of this phone number is too small
* to fill up its field, the field is padded with leading zeros.
* For example, if the value of the line number is 123, the last
* four characters of the string representation will be "0123".
*/
@Override
public String toString() {
return String.format("%03d-%03d-%04d",areaCode, prefix, lineNum);
}
  • 不指定格式:通过IDE或者Google AutoValue来自动生成

4 谨慎重写clone方法

(1)重写clone方法

  • 子类需要继承Cloneable接口,否则调用Object的clone方法将抛出CloneNotSupportedException。
  • 如果一个类实现了Cloneable接口,Object的clone方法将返回一个属性逐一拷贝的对象,否则它就抛出一个CloneNotSupportedException异常。
  • 一个类含有引用属性,需要进行深度克隆
public class Stack implement Cloneable{
private Object[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public Stack() {
this.elements = new Object[DEFAULT_INITIAL_CAPACITY];
} public void push(Object e) {
ensureCapacity();
elements[size++] = e;
} public Object pop() {
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null;
} private void ensureCapacity() {
if (elements.length == size)
elements = Arrays.copyOf(elements, 2 * size + 1);
} @Override
public Stack clone() {
try {
Stack result = (Stack) super.clone();
result.elements = elements.clone(); // 引用属性进行深度克隆
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}

5 考虑实现Comparable或使用Comparator

  • 一个对象在与指定的对象比较顺序时,当该对象小于,等于或者大于指定的对象时,相应地返回一个负的整型值,0或者正的整型值。
  • 若指定对象的类型不能与发起比较的对象进行比较,则抛出ClassCastException异常。
  • 具有自反性,对称性和传递性。
  • 强烈建议(x.compareTo(y) == 0) == (x.equals(y))成立(非绝对,比如:BigDecimal类)
  • compareTo方法中的基本数据类型比较,避免使用+ - > < 运算符,应该使用包装类的比较方法,如:Double.compare。(+ - 可能导致溢出)
  • Comparator示例:
private static final Comparator<PhoneNumber> COMPARATOR =
comparingInt((PhoneNumber pn) -> pn.areaCode)
.thenComparingInt(pn -> pn.prefix)
.thenComparingInt(pn -> pn.lineNum); public int compareTo(PhoneNumber pn) {
return COMPARATOR.compare(this, pn);
}

最新文章

  1. LBaaS 实现机制 - 每天5分钟玩转 OpenStack(125)
  2. jquery 监听常用监听方法
  3. php将html转成word文档下载
  4. PHP采集curl应用的一点小疑惑
  5. PhoneGap 在eclipse上开发Android程序
  6. Java 利用SWFUpload多文件上传 session 为空失效,不能验证的问题 swfUpload多文件上传
  7. shell 学习
  8. 网络协议 10 - Socket 编程(上):实践是检验真理的唯一标准
  9. 2016 alictf Timer writeup
  10. day01计算机组成与操作系统
  11. create-react-app脚手架的安装和目录结构介绍
  12. Sqoop2安装
  13. 在远程登陆的主机上通过命令行源码编译安装 GNU M4、autoconf、automake 等程序
  14. 【小程序】wxs使用
  15. swift开发之--报错:Class &quot;***ViewController&quot; has no initializers
  16. vc14(vs2015) 编译php7 记录
  17. 杨辉三角-python
  18. LJ语录
  19. jacob使用入门及问题解析
  20. python文件目录操作大全

热门文章

  1. IDEA导航光标回退和前进快捷键失效
  2. Java并发包线程池之ForkJoinPool即ForkJoin框架(一)
  3. 002-创建型-04-建造者模式(Builder)、JDK1.7源码中的建造者模式、Spring中的建造者模式
  4. Git——起步(待续)
  5. PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*
  6. HTTP连接详解
  7. php-fpm优化参数介绍
  8. vs2015配置link.exe环境变量
  9. 【Leetcode_easy】783. Minimum Distance Between BST Nodes
  10. react中如何处理日期格式整理