浅拷贝和深拷贝的方法java_jdg粗中有细

浅拷贝和深拷贝的方法java_jdg粗中有细1.拷贝的引入(1)引用拷贝创建一个指向对象的引用变量的拷贝。例1:Teacherteacher=newTeacher(“Taylor”,26);Teacherotherteacher=teacher;System.out.println(teacher);System.out.println(otherteacher);输出结果:blog.Teacher@355da254bl

1.拷贝的引入

(1)引用拷贝

创建一个指向对象的引用变量的拷贝。

例1:

Teacher teacher = new Teacher("Taylor",26);
Teacher otherteacher = teacher;
System.out.println(teacher);
System.out.println(otherteacher);

输出结果:

blog.Teacher@355da254
blog.Teacher@355da254

结果分析:由输出结果可以看出,它们的地址值是相同的,那么它们肯定是同一个对象。teacher和otherteacher的只是引用而已,他们都指向了一个相同的对象Teacher(“Taylor”,26)。 这就叫做引用拷贝。

例1 图解:
这里写图片描述

(2)对象拷贝

创建对象本身的一个副本。

例2:

Teacher teacher = new Teacher("Swift",26);
Teacher otherteacher = (Teacher)teacher.clone();
System.out.println(teacher);
System.out.println(otherteacher);

输出结果:

blog.Teacher@355da254
blog.Teacher@4dc63996

结果分析:由输出结果可以看出,它们的地址是不同的,也就是说创建了新的对象, 而不是把原对象的地址赋给了一个新的引用变量,这就叫做对象拷贝。

例2 图解:
这里写图片描述

注:深拷贝和浅拷贝都是对象拷贝

2.浅拷贝

(1)定义:

被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。即对象的浅拷贝会对“主”对象进行拷贝,但不会复制主对象里面的对象。”里面的对象“会在原来的对象和它的副本之间共享。

简而言之,浅拷贝仅仅复制所考虑的对象,而不复制它所引用的对象

(2)浅拷贝实例:

例3:

public class ShallowCopy { 
   
    public static void main(String[] args) throws CloneNotSupportedException { 
   
        Teacher teacher = new Teacher();
        teacher.setName("Delacey");
        teacher.setAge(29);

        Student2 student1 = new Student2();
        student1.setName("Dream");
        student1.setAge(18);
        student1.setTeacher(teacher);

        Student2 student2 = (Student2) student1.clone();
        System.out.println("拷贝后");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());
        System.out.println("修改老师的信息后-------------");

        // 修改老师的信息
        teacher.setName("Jam");
        System.out.println(student1.getTeacher().getName());
        System.out.println(student2.getTeacher().getName());
    }

}

class Teacher implements Cloneable { 
   
    private String name;
    private int age;

    public String getName() { 
   
        return name;
    }

    public void setName(String name) { 
   
        this.name = name;
    }

    public int getAge() { 
   
        return age;
    }

    public void setAge(int age) { 
   
        this.age = age;
    }
}

class Student2 implements Cloneable{ 
   
    private String name;
    private int age;
    private Teacher teacher;

    public String getName() { 
   
        return name;
    }

    public void setName(String name) { 
   
        this.name = name;
    }

    public int getAge() { 
   
        return age;
    }

    public void setAge(int age) { 
   
        this.age = age;
    }

    public Teacher getTeacher() { 
   
        return teacher;
    }

    public void setTeacher(Teacher teacher) { 
   
        this.teacher = teacher;
    }

    @Override
    public Object clone() throws CloneNotSupportedException { 
   
        Object object = super.clone();
        return object;
    }

}

输出结果:

拷贝后
Dream
18
Delacey
29
修改老师的信息后-------------
Jam
Jam

结果分析: 两个引用student1和student2指向不同的两个对象,但是两个引用student1和student2中的两个teacher引用指向的是同一个对象,所以说明是浅拷贝。

例3 图解:
这里写图片描述

3.深拷贝

(1)定义:

深拷贝是一个整个独立的对象拷贝,深拷贝会拷贝所有的属性,并拷贝属性指向的动态分配的内存。当对象和它所引用的对象一起拷贝时即发生深拷贝。深拷贝相比于浅拷贝速度较慢并且花销较大。

简而言之,深拷贝把要复制的对象所引用的对象都复制了一遍。

(2)实现深拷贝(实例1):

例4:

public class DeepCopy { 
   
    public static void main(String[] args) throws Exception { 
   
        Teacher2 teacher = new Teacher2();
        teacher.setName("Delacey");
        teacher.setAge(29);

        Student3 student1 = new Student3();
        student1.setName("Dream");
        student1.setAge(18);
        student1.setTeacher(teacher);

        Student3 student2 = (Student3) student1.clone();
        System.out.println("拷贝后");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());
        System.out.println("修改老师的信息后-------------");

        // 修改老师的信息
        teacher.setName("Jam");
        System.out.println(student1.getTeacher().getName());
        System.out.println(student2.getTeacher().getName());
    }
}

class Teacher2 implements Cloneable { 
   
    private String name;
    private int age;

    public String getName() { 
   
        return name;
    }

    public void setName(String name) { 
   
        this.name = name;
    }

    public int getAge() { 
   
        return age;
    }

    public void setAge(int age) { 
   
        this.age = age;
    }

    @Override
    public Object clone() throws CloneNotSupportedException { 
   
        return super.clone();
    }

}

class Student3 implements Cloneable { 
   
    private String name;
    private int age;
    private Teacher2 teacher;

    public String getName() { 
   
        return name;
    }

    public void setName(String name) { 
   
        this.name = name;
    }

    public int getAge() { 
   
        return age;
    }

    public void setAge(int age) { 
   
        this.age = age;
    }

    public Teacher2 getTeacher() { 
   
        return teacher;
    }

    public void setTeacher(Teacher2 teacher) { 
   
        this.teacher = teacher;
    }

    @Override
    public Object clone() throws CloneNotSupportedException { 
   
        // 浅复制时:
        // Object object = super.clone();
        // return object;

        // 改为深复制:
        Student3 student = (Student3) super.clone();
        // 本来是浅复制,现在将Teacher对象复制一份并重新set进来
        student.setTeacher((Teacher2) student.getTeacher().clone());
        return student;
    }

}

输出结果:

拷贝后
Dream
18
Delacey
29
修改老师的信息后-------------
Jam
Delacey

结果分析:
两个引用student1和student2指向不同的两个对象,两个引用student1和student2中的两个teacher引用指向的是两个对象,但对teacher对象的修改只能影响student1对象,所以说是深拷贝。

例4 图解1(teacher姓名Delacey更改前):

这里写图片描述

例4 图解2(teacher姓名Jam更改后):

这里写图片描述

(3)利用序列化实现深拷贝(实例2)

例5:

public class DeepCopyServiable { 
   
    public static void main(String[] args) throws Exception { 
   
        Teacher3 t = new Teacher3();
        t.setName("Taylor");
        t.setAge(28);

        Student3 s1 = new Student3();
        s1.setAge(20);
        s1.setName("blank space");
        s1.setTeacher(t);

        Student3 s2 = (Student3) s1.deepClone();

        System.out.println("拷贝后:");
        System.out.println(s2.getName());
        System.out.println(s2.getAge());
        System.out.println(s2.getTeacher().getName());
        System.out.println(s2.getTeacher().getAge());
        System.out.println("---------------------------");

        t.setName("swift");

        System.out.println("修改后:");
        System.out.println(s1.getTeacher().getName());
        System.out.println(s2.getTeacher().getName());
    }
}

class Teacher3 implements Serializable { 
   
    private String name;
    private int age;

    public String getName() { 
   
        return name;
    }

    public void setName(String name) { 
   
        this.name = name;
    }

    public int getAge() { 
   
        return age;
    }

    public void setAge(int age) { 
   
        this.age = age;
    }

}

class Student3 implements Serializable { 
   
    private String name;
    private int age;
    private Teacher3 teacher;

    public String getName() { 
   
        return name;
    }

    public void setName(String name) { 
   
        this.name = name;
    }

    public int getAge() { 
   
        return age;
    }

    public void setAge(int age) { 
   
        this.age = age;
    }

    public Teacher3 getTeacher() { 
   
        return teacher;
    }

    public void setTeacher(Teacher3 teacher) { 
   
        this.teacher = teacher;
    }

    public Object deepClone() throws Exception { 
   
        // 序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        oos.writeObject(this);

        // 反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);

        return ois.readObject();
    }
}

输出结果:

拷贝后:
blank space
20
Taylor
28
---------------------------
修改后:
swift
Taylor

结果分析:说明用序列化的方式实现了对象的深拷贝

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234284.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 电脑pin码忘了登录不进系统_做好这些优化,电脑5秒开机不是梦[通俗易懂]

    电脑pin码忘了登录不进系统_做好这些优化,电脑5秒开机不是梦[通俗易懂]为保证及时获取最新消息建议置顶公众号编辑:王富国审核:瓦彦文昨天公众号没有发文章,一是因为没有动力,毕竟写了文章没有回报,看不到任何的反馈,还有什么意思?二是因为我帮我一个朋友处理了一下她的电脑,也耗费了不少时间,也从中又得到了一些对电脑优化的经验,现在也给大家分享一下。本文较长,请耐心看完!昨天我这位朋友的电脑是全新的,对,刚买的,她自己看的,也是让我比较后,她自己选择购买的,配置大…

    2022年8月12日
  • 第一章,安装spark集群,测试和运行wordcount 案例

    第一章,安装spark集群,测试和运行wordcount 案例第一章,安装spark集群,测试和运行wordcount 案例

    2022年4月23日
  • 推荐算法中的MF, PMF, BPMF[通俗易懂]

    推荐算法中的MF, PMF, BPMF[通俗易懂]1.矩阵分解(MF)目前推荐系统中用的最多的就是矩阵分解方法,在NetflixPrize推荐系统大赛中取得突出效果。以用户-项目评分矩阵为例,矩阵分解就是预测出评分矩阵中的缺失值,然后根据预测值以某种方式向用户推荐。常见的矩阵分解方法有基本矩阵分解(basicMF),正则化矩阵分解)(RegularizedMF),基于概率的矩阵分解(PMF)等。利用代数中SVD方法对矩阵进行分解。奇异值

    2022年5月22日
  • winscp链接linux网络错误被决绝,解决了winscp连接不上的问题

    winscp链接linux网络错误被决绝,解决了winscp连接不上的问题在windows系统和虚拟机装的linux上互传文件时,可以用winscp。第一次用winscp时,老是连接不到linux上。但是两个系统都可以上网,还能ping通。还以为是端口22被什么进程占用。我的是windows2003的,虚拟机是redhatlinux9.0的。昨天下午又试试了,就解决这个问题啦。2.把linux里的防火墙给关了。步骤:在终端输入setup,在防火墙选择处,选择“无防…

    2022年9月15日
  • JavaSE基础(32) 遍历数组的3种方式

    JavaSE基础(32) 遍历数组的3种方式第一种:for循环//遍历数组publicclassThroughTheArray{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub int[]arr={12,4,1,66,54,6,74,-3};//静态创建一个数组 for(inti=0;i<…

    2022年9月18日
  • jquery setvalue_jquery on方法

    jquery setvalue_jquery on方法setInterval()方法<!DOCTYPEhtml><html><headlang=”en”><metacharset=”UTF-8″><title></title><scriptsrc=”jquery.js”></script><st…

    2022年9月7日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号