设计模式-创建型-原型模式-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);
    }
}

演示结果

相关推荐
苏-言4 分钟前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
界面开发小八哥11 分钟前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具
草莓base25 分钟前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Allen Bright38 分钟前
maven概述
java·maven
编程重生之路40 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
薯条不要番茄酱40 分钟前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
努力进修1 小时前
“探索Java List的无限可能:从基础到高级应用“
java·开发语言·list
politeboy1 小时前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
Daniel 大东2 小时前
BugJson因为json格式问题OOM怎么办
java·安全
哪 吒6 小时前
最简单的设计模式,抽象工厂模式,是否属于过度设计?
设计模式·抽象工厂模式