简单的属性值的复制只能实现浅拷贝
a.obj = b.obj
的方法,对于对象中的引用类型并不能真正的拷贝过来,只是复制其引用对象的地址到新的对象中
通过序列化实现深拷贝
父类
/**
* 汽车类
*/
public class Car {
public int height;
public int weight;
public Date outDay;
}
将被引用的类
/**
* 轮胎类
*/
public class LunTai implements Serializable {
public float r=30;
public float height=10;
/**
* 需要更大的规格
*/
public void bigger(){
this.r *= 1.5;
this.height *= 1.5;
}
/**
* 需要更小的规格
*/
public void smaller(){
this.r /= 1.5;
this.height /= 1.5;
}
}
用到引用的类:这个类会被用来拷贝
/**
* 通过序列化完成深拷贝
*/
public class MySuv extends Car implements Cloneable, Serializable {
public LunTai lunTai;
public MySuv(){
//仅完成初始化
this.outDay = new Date();
this.lunTai = new LunTai();
}
@Override
protected Object clone() throws CloneNotSupportedException {
return this.deepClone();
}
/**
* 深度拷贝:先序列化自己,再通过反序列化重建自己
*/
public Object deepClone(){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
MySuv mySuvClone = (MySuv) ois.readObject();
return mySuvClone;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
/**
* 浅克隆:引用类型只复制地址
*/
public MySuv shallowClone(MySuv target){
MySuv mySuv = new MySuv();
mySuv.height = target.height;
mySuv.weight = target.weight;
mySuv.lunTai = target.lunTai;
mySuv.outDay = target.outDay;
return mySuv;
}
}
测试
public class SUVTest {
public static void main(String[] args) {
MySuv mySuv = new MySuv();
System.out.println("原始的轮胎"+mySuv.lunTai);
try {
MySuv deep = (MySuv) mySuv.clone();//深拷贝
System.out.println("深拷贝后得到的轮胎:"+deep.lunTai);
System.out.println("深拷贝后得到的轮胎地址与原地址相同?:"+(deep.lunTai==mySuv.lunTai));
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
MySuv qian = mySuv.shallowClone(mySuv);//浅拷贝
System.out.println("浅拷贝后得到的轮胎:"+qian.lunTai);
System.out.println("浅拷贝后得到的轮胎地址与原地址相同?:"+(qian.lunTai==mySuv.lunTai));
}
}
结果
通过结果可以清晰看出序列化完成了深拷贝
注:序列化容易破坏单例
正因为序列化可以实现深拷贝,故如果对一个单例进行深拷贝,则会破坏单例。所以为了防止其破坏单例,需要禁止单例对象深克隆,即要么不要在单例对象中实现Cloneable接口,要么直接重写clone方法
protected Object clone(){
return this.INSTANCE;
}