- 冒号空间 - https://blog.zhenghui.org -

实现继承问题的经典实例

“You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.”

— from Effective Java, by Joshua Bloch


《冒号课堂》中提到了实现继承的诸多问题,必须引起重视,解决的方法其中之一便是将这类实现相关的“潜规则”明确写入文档。我觉得最经典的实例就是Java中覆盖equals()方法必须同时覆盖hashCode()的问题。


Share