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 +
                '}';
    }
}

<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;
    }

<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 +
                '}';
    }
}


版权声明:本文为weixin_43453386原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。