一、介绍
原型模式(Prototype),用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。
二、代码实现
public class Resume implements Cloneable{ private String name; private String sex; private String age; private String timeArea; private String company; public Resume(String name) { this.name = name; } //设置个人信息 public void setPersonalInfo(String sex, String age) { this.sex = sex; this.age = age; } //设置工作经历 public void setWorkExperience(String timeArea, String company) { this.timeArea = timeArea; this.company = company; } //展示简历 public void display() { System.out.println(this.name + "" + this.sex + "" + this.age); System.out.println("工作经历" + this.timeArea + " " + this.company); } @Override protected Resume clone() { try { return (Resume)super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } } }
public static void main(String[] args) { Resume resume1=new Resume("zhangsan"); Resume resume2 = resume1.clone(); Resume resume3 = resume1.clone(); System.out.println(resume1); }