• Java——》对象如何进行拷贝


    推荐链接:
        总结——》【Java】
        总结——》【Mysql】
        总结——》【Spring】
        总结——》【SpringBoot】
        总结——》【MyBatis、MyBatis-Plus】

    1、浅拷贝 = 浅克隆

    1)特点

    被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。

    2)实现方式

    <1> Object.clone()

    只是拷贝本对象 , 其对象内部的数组、引用对象等都不拷贝 ,还是指向原生对象的内部元素地址

    <2> Cloneable,Serializable

    被克隆的对象必须Cloneable,Serializable这两个接口

    package com.example.test.prototype;
    
    import java.io.Serializable;
    import java.util.Date;
    
    public class CloneForShallow implements Cloneable, Serializable {
    
        private String name;
    
        private Date birth;
    
        private int age;
    
        /**
         * 实现克隆的方法
         * @return
         * @throws CloneNotSupportedException
         */
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Date getBirth() {
            return birth;
        }
    
        public void setBirth(Date birth) {
            this.birth = birth;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public static void main(String[] args) throws Exception {
            Date date =  new Date(666666);
            // 原型对象
            CloneForShallow prototypeObject = new CloneForShallow();
            prototypeObject.setName("小仙");
            prototypeObject.setAge(18);
            prototypeObject.setBirth(date);
            System.out.println("原型对象的属性:" + prototypeObject);
            // 克隆对象
            CloneForShallow cloneObject = (CloneForShallow) prototypeObject.clone();
            System.out.println("克隆对象的属性:" + cloneObject);
            
            // 修改原型对象的属性
            date.setTime(12345677);
            // 修改克隆对象的属性
            cloneObject.setName("小仙~");
            
            System.out.println("原型对象的属性:" + prototypeObject);
            System.out.println("克隆对象的属性:" + cloneObject);
        }
    
        @Override
        public String toString() {
            return "CloneForShallow{" +
                    "name='" + name + '\'' +
                    ", birth=" + birth +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    <3> BeanUtils.copyProperties(Object source, Object target)

    2、深拷贝 = 深克隆

    1)特点

    被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。
    那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。
    换言之,深复制把要复制的对象所引用的对象都复制了一遍。

    2)实现方式:2种

    <1> 在浅克隆的基础上实现

        /**
         * 实现克隆的方法
         * @return
         * @throws CloneNotSupportedException
         */
        @Override
        protected Object clone() throws CloneNotSupportedException {
            CloneForDeep obj = (CloneForDeep) super.clone();
            // 实现深度克隆
            obj.birth = (Date) this.birth.clone();
            return obj;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    <2> 通过序列化和反序列化实现

    import java.io.*;
    import java.util.Date;
    
    public class CloneForDeep implements Cloneable, Serializable {
    
        private String name;
    
        private Date birth;
    
        private int age;
    
        /**
         * 实现克隆的方法
         *
         * @return
         * @throws CloneNotSupportedException
         */
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Date getBirth() {
            return birth;
        }
    
        public void setBirth(Date birth) {
            this.birth = birth;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public static void main(String[] args) throws Exception {
    
            Date date = new Date(666666);
            // 原型对象
            CloneForDeep prototypeObject = new CloneForDeep();
            prototypeObject.setName("小仙");
            prototypeObject.setAge(18);
            prototypeObject.setBirth(date);
            System.out.println("原型对象的属性:" + prototypeObject);
    
            //使用序列化和反序列化实现深复制
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(prototypeObject);
            byte[] bytes = bos.toByteArray();
    
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);
    
            // 克隆对象
            CloneForDeep cloneObject = (CloneForDeep) ois.readObject();
            // 修改原型对象的属性
            date.setTime(12345677);
            // 修改克隆对象的属性
            cloneObject.setName("小仙~");
            System.out.println("原型对象的属性:" + prototypeObject);
            System.out.println("克隆对象的属性:" + cloneObject);
        }
    
        @Override
        public String toString() {
            return "CloneForShallow{" +
                    "name='" + name + '\'' +
                    ", birth=" + birth +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    缓冲区设置
    PostgreSQL内存上下文
    Android4.4 Camera callback注册和回调过程分析
    今日头条 小程序
    [游戏开发][Unity] UGUI上使用 ParticleSystem支持排序渲染
    Linux中安装Docker,及docker中安装Mysql、Redis
    什么是PYTHONPATH,导包导模块究竟要怎么用才合理
    PyQt 定义控件SwitchButton 指南
    盘点54个Python实用工具源码Python爱好者不容错过
    简单评分程序(面向对象程序设计及C++)
  • 原文地址:https://blog.csdn.net/weixin_43453386/article/details/126761865