作为其一个痛苦的处理在两个地方之类的结构变化我经常这样做:使用中间阵列的hashCode和equals
class A {
class C{}
class B{}
private B bChild;
private C cChild;
private Object[] structure() {
return new Object[]{bChild, cChild};
}
public int hashCode() {
Arrays.hashCode(structure());
}
public boolean equals(Object that) {
//type check here
return Arrays.equals(this.structure(), ((A)that).structure());
}
}
有什么不好,除了原语的拳击这种做法? 可以改进吗?
除了事实,即它不能编译,你是什么意思?到目前为止,它没有任何建议。 – EJP
这种方法试图解决什么问题? – Vitaliy
@Vitaliy,将对象结构封装在两个地方而不是三个(fields,equals,hashCode)。 – Basilevs