• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Foreordination
酒后高歌磨剑,梦中快意恩仇,名利脚下踩,情义两肩挑
博客园    首页    新随笔    联系   管理    订阅  订阅
二十六、深拷贝与浅拷贝

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

 

posted on 2018-03-23 15:47  Foreordination  阅读(154)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3