JAVA的深拷贝

简单的属性值的复制只能实现浅拷贝

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值