- 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
- 就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创造的细节。
- 浅复制
-
public class WorkExperience{ private String timeArea, compary; public String getTimeArea() { return timeArea; } public void setTimeArea(String timeArea) { this.timeArea = timeArea; } public String getCompary() { return compary; } public void setCompary(String compary) { this.compary = compary; } } /** * super.clone(), * 如果字段是值类型,则对该字段执行逐位复制 * 如果字段是引用类型,则复制引用但不复制引用对象 * 因此,原始对象及其复本引用同一对象 * 浅复制指向的还是同一对象。所有的对其他对象的引用都仍然指向原来的对象。 */ public class Resume implements Cloneable { private String name, sex, age; private WorkExperience experience; /** * 无参构造方法 */ public Resume() { } /** * 有参构造方法 * * @param name */ public Resume(String name) { this.name = name; experience = new WorkExperience(); } public String getSex() { return sex; } /** * 设置个人信息 * * @param sex * @param age */ public void SetPersonalInfo(String sex, String age) { this.sex = sex; this.age = age; } public WorkExperience getExperience() { return experience; } /** * 设置工作经历 * * @param timeArea * @param compary */ public void SetWorkExperience(String timeArea, String compary) { experience.setCompary(compary); experience.setTimeArea(timeArea); } public void Display() { System.out.println(name + " " + sex + " " + age); System.out.println("工作经历 " + experience.getTimeArea() + " " + experience.getCompary()); } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } } /** * 通过R1和R2的getExperience()、R1和R3的getExperience()对象比较可以看出,R1、R2、R3尽管显示了不同的数据,但是还是只想同一个对象 * 通过R1和R2的getSex()、R1和R3的getSex()对象比较可以看出,R2在设置新值后显示与R1指向不同的对象,R3没有设置新的值则还是显示同一个对象。 */ public class TestUtil { public static void main(String[] args) throws CloneNotSupportedException { Resume r1 = new Resume("rabbit1"); r1.SetPersonalInfo("女", "18"); r1.SetWorkExperience("2001", "柠檬客"); r1.Display(); Resume r2 = (Resume) r1.clone(); r2.SetPersonalInfo("男", "19"); r2.SetWorkExperience("2003", "柠檬客1"); r2.Display(); System.out.println("R1和R2比较:" + (r1.getExperience() == r2.getExperience())); System.out.println("R1和R2的name比较:" + (r1.getSex() == r2.getSex())); Resume r3 = (Resume) r1.clone(); r2.SetPersonalInfo("女", "18"); r2.SetWorkExperience("2004", "柠檬客2"); r2.Display(); System.out.println("R1和R2比较:" + (r1.getExperience() == r3.getExperience())); System.out.println("R1和R2的name比较:" + (r1.getSex() == r2.getSex())); } }
-
- 深复制
-
public class WorkExperience implements Cloneable { private String timeArea, compary; public String getTimeArea() { return timeArea; } public void setTimeArea(String timeArea) { this.timeArea = timeArea; } public String getCompary() { return compary; } public void setCompary(String compary) { this.compary = compary; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } } /** * 实现原型模式的深复制 */ public class Resume implements Cloneable { private String name, sex, age; private WorkExperience experience; /** * 无参构造方法 */ public Resume() { } /** * 有参构造方法 * * @param name */ public Resume(String name) { this.name = name; experience = new WorkExperience(); } private Resume(WorkExperience experience) throws CloneNotSupportedException { this.experience = (WorkExperience) experience.clone(); } public String getSex() { return sex; } /** * 设置个人信息 * * @param sex * @param age */ public void SetPersonalInfo(String sex, String age) { this.sex = sex; this.age = age; } public WorkExperience getExperience() { return experience; } /** * 设置工作经历 * * @param timeArea * @param compary */ public void SetWorkExperience(String timeArea, String compary) { experience.setCompary(compary); experience.setTimeArea(timeArea); } public void Display() { System.out.println(name + " " + sex + " " + age); System.out.println("工作经历 " + experience.getTimeArea() + " " + experience.getCompary()); } @Override public Object clone() throws CloneNotSupportedException { Resume r = new Resume(this.experience); r.name = this.name; r.sex = this.sex; r.age = this.age; return r; } } /** * */ public class TestUtil { public static void main(String[] args) throws CloneNotSupportedException { Resume r1 = new Resume("rabbit1"); r1.SetPersonalInfo("女", "18"); r1.SetWorkExperience("2001", "柠檬客"); r1.Display(); Resume r2 = (Resume) r1.clone(); r2.SetPersonalInfo("男", "19"); r2.SetWorkExperience("2003", "柠檬客1"); r2.Display(); System.out.println("R1和R2比较:" + (r1.getExperience() == r2.getExperience())); System.out.println("R1和R2的name比较:" + (r1.getSex() == r2.getSex())); Resume r3 = (Resume) r1.clone(); r2.SetPersonalInfo("女", "18"); r2.SetWorkExperience("2004", "柠檬客2"); r2.Display(); System.out.println("R1和R2比较:" + (r1.getExperience() == r3.getExperience())); System.out.println("R1和R2的name比较:" + (r1.getSex() == r2.getSex())); } }
-