设计一个一个简历类,必须要有姓名,可以设置性别和年龄,可以设置工作经历。最终需要写三份简历。
public class Resume {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;thispany = company;}//展示简历public void display(){System.out.println(this.name+" "+this.sex+" "+this.age);System.out.println("工作经历"+this.timeArea+" "+thispany);}
}
Resume resume1 = new Resume("大鸟");resume1.setPersonalInfo("男","29");resume1.setWorkExperience("1998-2000","xx公司");Resume resume2 = new Resume("大鸟");resume2.setPersonalInfo("男","29");resume2.setWorkExperience("1998-2000","xx公司");Resume resume3 = new Resume("大鸟");resume3.setPersonalInfo("男","29");resume3.setWorkExperience("1998-2000","xx公司");resume1.display();resume2.display();resume3.display();
大鸟 男 29
工作经历1998-2000 xx公司
大鸟 男 29
工作经历1998-2000 xx公司
大鸟 男 29
工作经历1998-2000 xx公司
三份简历需要三次实例化。如果要二十份,就需要二十次实例化。因此,我们需要原型模式。
原型模式(Prototype),用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。
public class Prototype implements Cloneable{private String id;public Prototype(String id){this.id = id;}public String getID(){return this.id;}@Overrideprotected Object clone() {Object object = null;try {object = super.clone();}catch (CloneNotSupportedException e){println("Clone异常。");}return object;}
}
public class ConcretePrototype extends Prototype{public ConcretePrototype(String id){super(id);}
}
ConcretePrototype p1 =new ConcretePrototype("编号");System.out.println("原ID:"ID());ConcretePrototype c1 = (ConcretePrototype) p1.clone();System.out.println("原ID:"ID());
现在使用原型模式实现简历功能。
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;}...//省略一部分代码protected Resume clone(){Resume object = null;try {object = (Resume) super.clone();}catch (CloneNotSupportedException e){println("Clone异常");}return object;}
}
Resume resume1 = new Resume("大鸟");resume1.setPersonalInfo("男","29");resume1.setWorkExperience("1998-2000","XX公司");Resume resume2 = resume1.clone();resume2.setWorkExperience("2000-2003","YY集团");Resume resume3 = resume1.clone();resume3.setPersonalInfo("男","24");resume1.display();resume2.display();resume3.display();
原型模式的使用,不用重新初始化对象,而是动态地获得对象运行时的状态。这既隐藏了对象创建的细节,又对性能是大大的提高。
现在的简历类当中有一个设置工作经历的方法,在现实设计当中,一般会再有一个工作经历类,当中有时间区间和公司名称等属性,简历类直接调用这个对象即可。
public class WorkExperience{//工作时间范围private String timeArea;public String getTimeArea(){return this.timeArea;}public void setTimeArea(String value){this.timeArea = value;}//所在公司private String company;public String getCompany(){return thispany;}public void setCompany(String value){thispany = value;}
}
//简历类
public class Resume implements Cloneable{private String name;private String sex;private String age;private WorkExperience work; //声明一个工作经历的对象public Resume(String name){this.name = name;this.work = new WorkExperience();//对这个工作经历对象实例化}//设置个人信息public void setPersonalInfo(String sex,String age){this.sex = sex;this.age = age;}//摄制工作经历public void setWorkExperience(String timeArea,String company){this.work.setTimeArea(timeArea);this.work.setCompany(company);}//展示简历public void display(){System.out.println(this.name+" "+this.sex+" "+this.age);System.out.println("工作经历"+TimeArea()+" "+Company());}public Resume clone(){Resume object = null;try {object = (Resume) super.clone();}catch (CloneNotSupportedException e){println("Clone异常");}return object;}
}
Resume resume1 = new Resume("大鸟");resume1.setPersonalInfo("男","29");resume1.setWorkExperience("1998-2000","XX公司");Resume resume2 = resume1.clone();resume2.setWorkExperience("2000-2003","YY集团");Resume resume3 = resume1.clone();resume3.setPersonalInfo("男","24");resume1.display();resume2.display();resume3.display();
大鸟 男 29
工作经历2000-2003 YY集团
大鸟 男 29
工作经历2000-2003 YY集团
大鸟 男 24
工作经历2000-2003 YY集团
结果显示,前两次的工作经历数据被最后一次数据给覆盖了。这是因为clone方法是浅复制,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
但我们可能更需要这样的一种需求,把要复制的对象所引用的对象都复制一遍。这种叫做深复制,深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
public class WorkExperience implements Cloneable{//工作时间范围private String timeArea;public String getTimeArea(){return this.timeArea;}public void setTimeArea(String value){this.timeArea = value;}//所在公司private String company;public String getCompany(){return thispany;}public void setCompany(String value){thispany = value;}protected WorkExperience clone() {WorkExperience object = null;try {object = (WorkExperience) super.clone();}catch (CloneNotSupportedException e){println("Clone异常");}return object;}
}
package com.lzh.prototype.v4;public class Resume {private String name;private String sex;private String age;private WorkExperience work; //声明一个工作经历的对象public Resume(String name){this.name = name;this.work = new WorkExperience();//对这个工作经历对象实例化}//设置个人信息public void setPersonalInfo(String sex,String age){this.sex = sex;this.age = age;}//摄制工作经历public void setWorkExperience(String timeArea,String company){this.work.setTimeArea(timeArea);this.work.setCompany(company);}//展示简历public void display(){System.out.println(this.name+" "+this.sex+" "+this.age);System.out.println("工作经历"+TimeArea()+" "+Company());}public Resume clone(){Resume object = null;try {object = (Resume) super.clone();object.work = this.work.clone();}catch (CloneNotSupportedException e){println("Clone异常");}return object;}
}
客户端类同之前一样,输出结果如下。
大鸟 男 29
工作经历1998-2000 XX公司
大鸟 男 29
工作经历2000-2003 YY集团
大鸟 男 24
工作经历1998-2000 XX公司
本文发布于:2024-01-28 02:18:01,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063794894074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |