二十六、深拷贝与浅拷贝

Java中有两种类型变量,值类型和引用类型。

1、对于值类型:copy是属于全盘复制,也就是深拷贝

2、对于引用类型:一般的copy只是浅拷贝,相当于传递一个引用指针一样。

而当引用类型进行深拷贝的时候必须实现Cloneable接口中提供的Clone方法。

通俗的说:

深拷贝:不仅复制对象的基本类型,还复制原来对象中的对象,完全产生一个新的对象。

浅拷贝:只是赋值对象的基本类型,对象还是属于原来的引用。如果是值类型直接拷贝到新的对象,引用类型则是复制一个引用到目标对象而已。

在有指针的情况下,浅拷贝只是增加了一个指针指向已经存在的内存而已。

深拷贝不但增加了一个指针还申请了一个新的内存,让这个增加的指针指向新的内存。所以当采用深拷贝释放内存的时候不会出现重复释放同一个内存的错误。

浅拷贝代码如下:

public class Teacher implements Cloneable{
    private String name;
    private int age;
    
    Teacher(String name,int age){
        this.name=name;
        this.age=age;
    }
    
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}

public class Student implements Cloneable{
    private String name;
    private int age;
    private Teacher teacher;
    
    Student(String name,int age,Teacher teacher){
        this.name=name;
        this.age=age;
        this.teacher=teacher;
    }
    //重写clone方法
    public Object clone(){
        Student student=null;
        try{
            student=(Student)super.clone();
        }catch(CloneNotSupportedException e){
            e.printStack();
        }
        return student;
    }
}

//测试浅拷贝
public class ShallowCopy{
    public static void main(String[] args){
        Teacher teacher=new Teacher("流浪法师",50);
        Student student1=new Student("盖伦",30,teacher);
        Student student2=(Student)student1.clone();
        
        student2.teacher.name="提莫";
        student2.teacher.age=20;
        student2.name="卡特";
        student2.age=22;
        System.out.println("student1的姓名:"+student1.name+",student1的老师名字:"
        +student1.teacher.name+",student1的老师年龄:"+student1.teacher.age);//盖伦,提莫,20
        //发现student2变了,student1变了,说明student1和student2的teacher引用是指向同一个对象。
    }
}
View Code

深拷贝代码如下:

public class Teacher implements Cloneable{
    private String name;
    private int age;
    
    Teacher(String name,int age){
        this.name=name;
        this.age=age;
    }
    
    public Object clone() throws CloneNotSupportedException{
        Object o=null;
        try{
            object=super.clone();//复制一个对象,相当于申请一片新的内存空间用来存储对象
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return object;
    }
}

public class Student implements Cloneable{
    private String name;
    private int age;
    private Teacher teacher;
    
    Student(String name,int age,Teacher teacher){
        this.name=name;
        this.age=age;
        this.teacher=teacher;
    }
    //重写clone方法
    public Object clone(){
        Student student=null;
        try{
            student=(Student)super.clone();
        }catch(CloneNotSupportedException e){
            e.printStack();
        }
        student.teacher=(Teacher)teacher.clone();//进行深拷贝
        return student;
    }
}

//测试深拷贝
public class DeepCopy{
    public static void main(String[] args){
        Teacher teacher=new Teacher("流浪法师",50);
        Student student1=new Student("盖伦",30,teacher);
        Student student2=(Student)student1.clone();
        
        student2.teacher.name="提莫";
        student2.teacher.age=20;
        student2.name="卡特";
        student2.age=22;
        System.out.println("student1的姓名:"+student1.name+",student1的老师名字:"
        +student1.teacher.name+",student1的老师年龄:"+student1.teacher.age);//盖伦,流浪法师,50
        //发现student2变了,student1还是没变,
        //说明student1和student2的teacher引用不是指向同一个对象。
        //新增了一片存储空间,各自的引用指向了存储空间
    }
}
View Code

 

转载于:https://www.cnblogs.com/drq1/p/8630729.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值