首页>代码>java和js实现深拷贝与浅拷贝方案代码>/20230427-深拷贝与浅拷贝/deep-copy/src/com/cxs/DeepCopyTest.java
package com.cxs;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @Project: deep-copy
 * @Author: cxs2014501@163.com
 * @Create: 2023/4/27 10:15
 * @Description:
 **/
public class DeepCopyTest {
    public static void main(String[] args) {
        ObjectInputStream oin = null;
        ObjectOutputStream oos = null;
        ByteArrayOutputStream bos = null;
        ByteArrayInputStream bis = null;
        try {
            Person person = new Person("张三", 23, new Job("阿里巴巴", 30));
            Person personCopy = null;

            // 对象序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(person);

            // 对象反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            oin = new ObjectInputStream(bis);
            personCopy = (Person) oin.readObject();


            System.out.println("源数据" + person);
            System.out.println("克隆数据" + personCopy);

            person.setAge(20);
            person.getJob().setSalary(40);

            System.out.println("修改后源数据" + person);
            System.out.println("修改后克隆数据" + personCopy);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流资源,先开的后关
        }
    }
}


class Person implements Serializable {
    private String name;
    private Integer age;
    private Job job;

    public Person() {
    }

    public Person(String name, Integer age, Job job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job=" + job +
                '}';
    }
}

class Job implements Serializable {
    private String company;
    private Integer salary;

    public Job() {
    }

    public Job(String company, Integer salary) {
        this.company = company;
        this.salary = salary;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public Integer getSalary() {
        return salary;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Job{" +
                "company='" + company + '\'' +
                ", salary=" + salary +
                '}';
    }
}
最近下载更多
wanfeng_233  LV4 2023年5月29日
最代码官方  LV167 2023年5月14日
最近浏览更多
kenhomeliu  LV29 4月30日
wanglinddad  LV54 3月24日
sunlzh888888  LV28 3月16日
80730176  LV7 2月1日
ITkuangren  LV8 2023年9月26日
茶茶茶百道qq 2023年9月20日
暂无贡献等级
woldxy  LV12 2023年8月22日
newscc  LV8 2023年8月12日
别碰我被窝  LV9 2023年6月14日
Iterman  LV2 2023年6月14日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友