设计模式-创建型-原型模式-Prototype

工作经验类

java 复制代码
public class WorkExperience implements Cloneable {
    private String workDate;
    private String company;

    public void setWorkDate(String workDate) {
        this.workDate = workDate;
    }

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

    @Override
    public Object clone() throws CloneNotSupportedException {
        WorkExperience obj = new WorkExperience();
        obj.workDate = this.workDate;
        obj.company = this.company;
        return obj;
    }

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

简历类

java 复制代码
public class Resume implements Cloneable {
    private String name;
    private String sex;
    private String age;
    private WorkExperience work;

    public Resume(String name) {
        this.name = name;
        work = new WorkExperience();
    }

    private Resume(WorkExperience work) throws CloneNotSupportedException {
        this.work = (WorkExperience) work.clone();
    }

    public void setPersonInfo(String sex, String age) {
        this.sex = sex;
        this.age = age;
    }

    public void setWorkExperience(String workDate, String company) {
        work.setWorkDate(workDate);
        work.setCompany(company);
    }


    @Override
    public Object clone() throws CloneNotSupportedException {
        Resume obj = new Resume(this.work);
        obj.name = name;
        obj.sex = sex;
        obj.age = age;
        return obj;
    }

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

演示类

java 复制代码
public class WorkResume {
    public static void main(String[] args) throws CloneNotSupportedException {
        Resume a = new Resume("张三");
        a.setPersonInfo("男","29");
        a.setWorkExperience("1998~2000","XXX公司");
        System.out.println(a);
        Resume b= (Resume) a.clone();
        b.setWorkExperience("2001~2006","YYY公司");
        System.out.println(b);
    }
}

演示结果

相关推荐
confiself11 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
Wlq041516 分钟前
J2EE平台
java·java-ee
XiaoLeisj23 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
豪宇刘38 分钟前
SpringBoot+Shiro权限管理
java·spring boot·spring
Elaine20239142 分钟前
02多线程基础知识
java·多线程
gorgor在码农44 分钟前
Redis 热key总结
java·redis·热key
百事老饼干1 小时前
Java[面试题]-真实面试
java·开发语言·面试
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
2402_857589361 小时前
SpringBoot框架:作业管理技术新解
java·spring boot·后端