克隆
@Override
protected Object clone() throws CloneNotSupportedException {
DeepPrototype deepPrototype = null;
deepPrototype = (DeepPrototype) super.clone();
deepPrototype.target = (DeepCloneableTarget) this.target.clone();
return deepPrototype;
}
序列化(推荐)
- 实现
Serializable
- 定义
serialVersionUID
public DeepPrototype deepClone() {
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try{
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
DeepPrototype o = (DeepPrototype) ois.readObject();
return o;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (bos != null) bos.close();
if (oos != null) oos.close();
if (bis != null) bis.close();
if (ois != null) ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
将序列化的类输出到本地文件
public static void cloneFile() throws IOException, ClassNotFoundException {
FileOutputStream fos = new FileOutputStream("E:/ProgramData/Project/Object.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
DeepCloneableTarget target = new DeepCloneableTarget("李四", "张三");
oos.writeObject(target);
oos.flush();
oos.close();
fos.close();
FileInputStream fis = new FileInputStream("E:/ProgramData/Project/Object.out");
ObjectInputStream ois = new ObjectInputStream(fis);
target = (DeepCloneableTarget) ois.readObject();
ois.close();
fis.close();
System.out.println(target);
}
整体代码
public class DeepCloneableTarget implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
private String cloneName;
private String cloneClass;
public DeepCloneableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "DeepCloneableTarget{" +
"cloneName='" + cloneName + '\'' +
", cloneClass='" + cloneClass + '\'' +
'}';
}
}
public class DeepPrototype implements Serializable, Cloneable {
private static final long serialVersionUID = -1897843806793850888L;
public String name;
public DeepCloneableTarget target;
public DeepPrototype() {
}
@Override
protected Object clone() throws CloneNotSupportedException {
DeepPrototype deepPrototype = null;
deepPrototype = (DeepPrototype) super.clone();
deepPrototype.target = (DeepCloneableTarget) this.target.clone();
return deepPrototype;
}
public DeepPrototype deepClone() {
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try{
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
DeepPrototype o = (DeepPrototype) ois.readObject();
return o;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (bos != null) bos.close();
if (oos != null) oos.close();
if (bis != null) bis.close();
if (ois != null) ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}