用JAVA实现深复制

Resume 简历类

package com.springboot.girl.design.prototype;

import lombok.Data;

/**
 * @Description 简历
 * @Date 2019/6/26
 */
@Data
public class Resume implements Cloneable{
    private String name;
    private int age;
    private WorkExperience workExperience;

    public Resume(String name, int age, WorkExperience workExperience) {
        this.name = name;
        this.age = age;
        this.workExperience = workExperience;
    }

    @Override
    protected Resume clone() throws CloneNotSupportedException {
        Resume resume = (Resume) super.clone();
        WorkExperience we = resume.getWorkExperience();
        resume.setWorkExperience((WorkExperience) we.clone());
        return resume;
    }

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

WorkExperience

package com.springboot.girl.design.prototype;

import lombok.Data;

/**
 * @Description TODO
 * @Date 2019/6/26
 */
@Data
public class WorkExperience implements Cloneable{
    private String unitName;
    private String workTime;

    public WorkExperience(String unitName, String workTime) {
        this.unitName = unitName;
        this.workTime = workTime;
    }

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

    @Override
    public String toString() {
        return "WorkExperience{" + "unitName='" + unitName + '\'' + ", workTime='" + workTime + '\'' + '}';
    }
}

TestMain

package com.springboot.girl.design.prototype;

/**
 * @Description TODO
 * @Date 2019/6/26
 */
public class TestMain {

    public static void main(String[] args) throws CloneNotSupportedException {
        Resume r1 = new Resume("张三",23,new WorkExperience("联创","2016-2018"));
        Resume r2 = r1.clone();
        r2.getWorkExperience().setUnitName("投哪");
        r2.getWorkExperience().setWorkTime("2018-2019");
        System.out.println(r1);
        System.out.println(r2);
    }
}

 

运行结果

Resume{name='张三', age=23, workExperience=WorkExperience{unitName='联创', workTime='2016-2018'}}
Resume{name='张三', age=23, workExperience=WorkExperience{unitName='投哪', workTime='2018-2019'}}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值