在java中,实现深层对象复制功能有点困难.您采取了哪些步骤来确保原始对象和克隆的对象没有共享?
曾几何时有一堂课:
public class Scope<C extends Cloneable & Comparable<C>> implements Comparable<Scope<C>>, Cloneable, Serializable {
private C starts;
private C ends;
...
@SuppressWarnings("unchecked")
@Override
public Object clone() {
Scope<C> scope;
try {
scope = (Scope<C>) super.clone();
scope.setStarts((C) starts.clone()); // The method clone() from the type Object is not visible
scope.setEnds((C) ends.clone()); // The method clone() from the type Object is not visible
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone not supported");
}
return scope;
}
}
Run Code Online (Sandbox Code Playgroud)
在对象中我们有:
protected native Object clone() …Run Code Online (Sandbox Code Playgroud)