Effective Java 读书笔记(二):对象通用方法

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);
}
原文地址:https://www.cnblogs.com/linzhanfly/p/10191554.html