通用约定,下面的约定前提都是x/y/z不为null值。
诀窍:
“为不相等的对象产生不相等的散列码”,这正是hashCode约定中第三条的含义。计算hashCode示范代码:
@Override
public int hashCode() { int result = 17; result = 31 * result + areaCode; result = 31 * result + prefix; result = 31 * result + lineNumber; return result; }
31有个很好的特性,即用移位和减法来代替乘法,可以得到更好的性能:31*i == (i<<5)-i。现代的VM可以自动完成这种优化。
如果计算散列码的开销比较大,可以考虑缓存,只计算一次等方法。
“建议所有的子类都覆盖这个方法”。在实际应用中,toString方法应该返回对象中包含的所有值得关注的信息。
无论你是否决定指定格式,都应该在文档中明确地表明你的意图。示范:
/** * Returns the string representation of this phone number. * The string consists of fourteen 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". * * Note that there is a single space separating the closing * parenthesis after the area code from the first digit of the * prefix. */ @Override
public String toString() { return String.format("(%03d) %03d-%04d", areaCode, prefix, lineNumber); }
compareTo方法的通用约定与equals方法的相似:将这个对象与指定的对象进行比较。当该对象小于、等于或大于指定对象的时候,分别返回一个负整数、零或者正整数。如果由于指定对象的类型而无法与该对象进行比较,则抛出ClassCastException异常。
强烈建议(x.compareTo(y) == 0) == (x.equals(y)),但这并非绝对必要。
如果一个类有多个关键域,你必须从最关键的域开始,逐步进行到所有的重要域。
示范代码:
public int compareTo(PhoneNumber pn) { // Compare area codes int areaCodeDiff = areaCode - pn.areaCode; if (areaCodeDiff != 0) return areaCodeDiff; // Area codes are equal, compare prefixes int prefixDiff = prefix - pn.prefix; if (prefixDiff != 0) return prefixDiff; // Area codes and prefixes are equal, compare line numbers return lineNumber - pn.lineNumber; }