java中的深拷贝和浅拷贝

2017-11-20  本文已影响0人  zhouzhuo933

简单记录一下java中的深拷贝和浅拷贝,深拷贝和浅拷贝只是针对对象而言的.

1 深拷贝代码
public class DeepCopy implements Cloneable{

    public String text;
    public ArrayList<String> list = new ArrayList<>();

    public DeepCopy(){
        Log.d("zhouzhuo","------DeepCopy 构造函数------");
    }

    public void show(){
        Log.d("zhouzhuo","text=="+text);
        for (int i=0;i<list.size();i++){
            Log.d("zhouzhuo","list=="+list.get(i));
        }
    }

    public void add(String text){
        list.add(text);
    }

    @Override
    protected DeepCopy clone() {
        try {
            DeepCopy copy = (DeepCopy) super.clone();
            copy.text = this.text;
            copy.list = (ArrayList<String>)this.list.clone();//此处需要把list集合也拷贝一下
            return copy;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
2 浅拷贝代码

public class ShallowCopy implements Cloneable{
    public String text;

    public ArrayList<String> list = new ArrayList<>();
    public ShallowCopy(){
        Log.d("zhouzhuo","------ShallowCopy 构造函数------");
    }

    public void show(){
        Log.d("zhouzhuo","text=="+text);
        for (int i=0;i<list.size();i++){
            Log.d("zhouzhuo","list=="+list.get(i));
        }
    }

    public void add(String text){
        list.add(text);
    }

    @Override
    protected ShallowCopy clone() {
        try {
            ShallowCopy copy = (ShallowCopy) super.clone();
            copy.text = this.text;
            copy.list = this.list;//
            return copy;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}

3 测试代码


    public void deep(View view){
        Toast.makeText(MainActivity.this,"deep",Toast.LENGTH_SHORT).show();
        DeepCopy deepCopy = new DeepCopy();
        deepCopy.text = "deepCopy 之前";
        deepCopy.add("00000");

        DeepCopy deepCopy1 = deepCopy.clone();
        deepCopy1.text ="deepCopy 之后";
        deepCopy1.add("111");
        Log.d("zhouzhuo","============");
        deepCopy.show();
        Log.d("zhouzhuo","============");
        deepCopy1.show();
        Log.d("zhouzhuo","============");

    }


    public void shallow(View view){
        Toast.makeText(MainActivity.this,"shallow",Toast.LENGTH_SHORT).show();

        ShallowCopy shallowCopy = new ShallowCopy();
        shallowCopy.text = "shallowCopy 之前";
        shallowCopy.add("00000");
        ShallowCopy shallowCopy1 = shallowCopy.clone();
        shallowCopy1.text ="shallowCopy 之后";
        shallowCopy1.add("111");
        Log.d("zhouzhuo","============");
        shallowCopy.show();
        Log.d("zhouzhuo","============");
        shallowCopy1.show();
        Log.d("zhouzhuo","============");
    }

4 打印结果

4:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ------DeepCopy 构造函数------
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ============
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: text==deepCopy 之前
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: list==00000
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ============
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: text==deepCopy 之后
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: list==00000
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: list==111
11-20 10:14:48.235 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ============
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ------ShallowCopy 构造函数------
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ============
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: text==shallowCopy 之前
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: list==00000
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: list==111
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ============
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: text==shallowCopy 之后
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: list==00000
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: list==111
11-20 10:14:56.830 17973-17973/com.zhouzhuo.longcopy D/zhouzhuo: ============
11-20 10:15:30.510 17973-17973/com.zhouzhuo.longcopy D/dalvikvm: MakeBoost:0x4160d2c8 to 0
5 分析

通过打印的结果可以区分出深拷贝和浅拷贝的区别。不管是深拷贝和浅拷贝都不会执行构造函数。深拷贝只对引用类型起作用 。deepCopy1.text改变之后 ,deepCopy.text 也随着改变了。所以说深拷贝对于基本类型没有起作用。父本会随着副本的改变而改变。

代码下载
上一篇下一篇

猜你喜欢

热点阅读