浅拷贝 和 深拷贝
浅拷贝:只复制一层,如果对象的属性是引用数据类型,只会复制属性内存地址。
深拷贝:不只复制一层,如果对象属性是引用数据类型,会继续向下进行复制。
深拷贝的实现方法
1. 实现 Cloneable 接口
Artist.class
public class Artist implements Cloneable{
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private int age;
public Artist(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public Artist clone() {
try {
Artist clone = (Artist) super.clone();
// TODO: copy mutable state here, so the clone can't change the internals of the original
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
Music.class
public class Music implements Cloneable{
/**
* 音乐名称
*/
private String title;
/**
* 所属专辑
*/
private String album;
/**
* 作曲人
*/
private Artist artist;
public Music(String title, String album, Artist artist) {
this.title = title;
this.album = album;
this.artist = artist;
}
@Override
public Music clone() {
try {
Music clone = (Music) super.clone();
// TODO: copy mutable state here, so the clone can't change the internals of the original
clone.artist = this.artist.clone();
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
Music 类中的 artist 属性是 Artist类,Music 和 Artist 都要实现 Cloneable 接口,并且重写 clone() 方法
2. 序列化
每一个背拷贝的类都要实现 Serializable 接口
//深度拷贝
public Object deepClone() throws Exception{
// 序列化
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
// 反序列化
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
参考:https://www.cnblogs.com/ysocean/p/8482979.html
标签:Java,Artist,实现,clone,artist,Music,拷贝,public From: https://www.cnblogs.com/HypoPine/p/16982540.html