【发布时间】:2012-03-20 17:54:22
【问题描述】:
public class Node {
private Node nextNode;
@Override
public int hashCode() {
//How to implement this?
//Because you just have a attribute which is a reference.
//I think the attribute is almost useless, because if you use the HashCode of the attribute, you will finally fall into a useless loop.
//Thus, I think you should find a way to represent the HashCode of reference (instance) itself.
}
}
从代码中的注释来看,我的问题实际上是如何唯一标识引用本身,比如 C 中的地址。
【问题讨论】:
-
我猜这应该是一个基类。如果是这样,请不要费心实现它,因为仅指向下一个节点的节点没有用,是吗?我猜你永远不会在不扩展它的情况下使用它。也许让类抽象来强制执行这一点?
-
如果你确实想覆盖哈希码,正确的声明是
public int hashCode() {}