考虑一个类,对超类隐藏成员。如果实现克隆,那么如何正确更新两个成员?
public class Wrapper implements Cloneable{
protected Collection core;
protected Wrapper(Collection core) {
this.core = core;
}
public Wrapper clone() {
try {
Wrapper ans = (Wrapper) super.clone();
ans.core = (Collection) core.getClass().newInstance();
for(Object o : core) {
ans.core.add( o.clone() );
}
return ans;
}
catch(CloneNotSupportedException e) {
throw new AssertionError(e);
}
}
}
public class Child extend Wrapper {
protected ArrayList core; // for simpler access
public Child() {
super(new ArrayList());
this.core = (ArrayList) super.core;
}
public Child clone() {
Child ans = (Child) super.clone();
ans.core ... // how to update both core members?
// ans.super.core ... ?
// ans.this.core ... ?
}
}