Рассмотрите класс, скрывающий член от суперкласса. Если вы выполняете клонирование, то как правильно обновлять оба элемента?Возможно ли получить доступ к скрытому полю из внешнего объекта в Java?
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 ... ?
}
}