深拷贝
浅拷贝的问题
class Writer implements Cloneable{
private int age;
private String name;
// getter/setter 、构造方法、toString()
}public interface Cloneable {
}class TestClone {
public static void main(String[] args) throws CloneNotSupportedException {
Writer writer1 = new Writer(18,"二哥");
Writer writer2 = (Writer) writer1.clone();
System.out.println("浅拷贝后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
writer2.setName("三妹");
System.out.println("调整了 writer2 的 name 后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
}
}

实现深拷贝

最后更新于