java中的浅复制与深复制

java中的浅复制与深复制

笔者在完全理解java中的浅复制与深复制之前,有参考过很多其他网页及博客等,限于篇幅原因,未能记录。直接上代码

代码块

package test;

public class B implements Cloneable{//一定要继承此接口
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
    public B(String str){
        this.str=str;
    }

    @Override
    public B clone() throws CloneNotSupportedException{
        B b=null;
        b=(B)super.clone();
        return b;
    }
}
package test;

public class A implements Cloneable{
    public String str;//判断在clone时候,字符串类型数据是否需要单独再new一下
    public Double d;//同上,判断Double类型数据
    public Integer i;//同上,判断Integer类型数据
    public B b;//同上,判断Object B类型

    public A(String str,Double d,Integer i,B b){
        this.str=str;
        this.d=d;
        this.i=i;
        this.b=b;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Double getD() {
        return d;
    }

    public void setD(Double d) {
        this.d = d;
    }

    public Integer getI() {
        return i;
    }

    public void setI(Integer i) {
        this.i = i;
    }

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }

    @Override
    public String toString(){
        return String.format("the Double is %f, the Integer is %d, the String is %s, and the object B is %s", this.d,this.i,this.str,this.b.getStr());
    }

    @Override
    public A clone() throws CloneNotSupportedException{
        A a=null;
        a=(A)super.clone();
        if(b!=null){
            a.b=(B)b.clone();
        }
        return a;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        B b1=new B("b1");
        A a1=new A("a1", 1.0, 1, b1);
        System.out.println(a1.toString());
        A a2=a1.clone();
        System.out.println(a2.toString());
        System.out.println(String.format("the String equal is %b, the Double equal is %b, the Integer equal is %b, the Object B equal is %b ", a1.str==a2.str, a1.d==a2.d, a1.i==a2.i, a1.b==a2.b));

    }

}

运行结果
由此证明,对于String、Double、Integer这样的基本类型的封装类,在clone方法中也是需要像B类型变量那样进行操作。下面对list类型的成员变量进行clone。

package test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AA implements Cloneable{
    private List<Integer> list;
    private List<B> b_list;

    public List<Integer> getList() {
        return list;
    }
    public void setList(List<Integer> list) {
        this.list = list;
    }
    public List<B> getB_list() {
        return b_list;
    }
    public void setB_list(List<B> b_list) {
        this.b_list = b_list;
    }
    @Override
    public String toString(){
        String list_temp="";
        if(list!=null)
        for(Integer i :list){
            list_temp+=i+",";
        }
        String listb_list="";
        if(b_list!=null)
        for(B i :b_list){
            listb_list+=i.getStr()+",";
        }
        return String.format("the list is %s , and b_list is %s", list_temp,listb_list);
    }

    @Override
    public AA clone() throws CloneNotSupportedException{
        AA a=null;
        a=(AA)super.clone();
        if(list!=null){
            List<Integer> list_copy=new ArrayList();
            Iterator<Integer> iterator=list.iterator();
            while(iterator.hasNext()){
                //list_copy.add(iterator.next());
                list_copy.add(new Integer(iterator.next()));
            }
            a.list=list_copy;
        }
        if(b_list!=null){
            List<B> list_copy=new ArrayList<B>();
            Iterator<B> iterator=b_list.iterator();
            while(iterator.hasNext()){
                //list_copy.add(iterator.next());
                list_copy.add(iterator.next().clone());
            }
            a.b_list=list_copy;
        }
        return a;
    }

    public static void main(String[] args) throws CloneNotSupportedException{
        AA one=new AA();

        ArrayList<Integer> list=new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        one.setList(list);

        List<B> b_list=new ArrayList<B>();
        B b1=new B("b1");
        b_list.add(b1);
        B b2=new B("b2");
        b_list.add(b2);
        one.setB_list(b_list);

        System.out.println("one :"+one.toString());
        AA two=one.clone();
        list.add(4);
        list.set(0, new Integer(11111));
        b1.setStr("BBBBBB");
        System.out.println("one :"+one.toString());
        System.out.println("two :"+two.toString());
        System.out.println(String.format("the one and two's List is sample addr? %b", one.list==two.list));
        System.out.println(String.format("the one and two's B_List is sample addr? %b", one.b_list==two.b_list));
    }
}

运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值