以简历类为例:
一、简历代码初步实现
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;
this.company=company;
}
public void Display() {
System.out.println(name+" "+sex+" "+age);
System.out.println("工作经历:"+timeArea+" "+company);
}
}
public class Main{
public static void main(String[] args) {
Resume a=new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b=new Resume("大鸟");
b.setPersonalInfo("男", "29");
b.setWorkExperience("1998-2000", "XX公司");
Resume c=new Resume("大鸟");
c.setPersonalInfo("男", "29");
c.setWorkExperience("1998-2000", "XX公司");
a.Display();
b.Display();
c.Display();
}
}
此代码几分简历就需要几次实例化,客户端代码较麻烦。
public class Main{
public static void main(String[] args) {
Resume a=new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b=a;
Resume c=a;
a.Display();
b.Display();
c.Display();
}
}
用传引用改进,相当于在b纸张和c纸张上写着简历在a纸张上。
二、原型模式
abstract class Prototype { // 原型类
private String id;
public Prototype(String id) {
this.id = id;
}
public void setId(String id) {
this.id = id;
}
public abstract Prototype Clone();
}
class ConcretePrototype1 extends Prototype {// 具体原型类
private String id;
public ConcretePrototype1(String id) {
super(id);
}
@Override
public Prototype Clone() {
Prototype prototype = new ConcretePrototype1(id);
prototype.setId(this.id);
System.out.println("ConcretePrototype1创建完成");
return prototype;
}
}
public class Main {
public static void main(String[] args) {
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1) p1.Clone();
System.out.println("Cloned:" + c1);
}
}
用原型模式改进:
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(name+" "+sex+" "+age);
System.out.println("工作经历:"+timeArea+" "+company);
}
public Resume clone() {
try {
return (Resume)super.clone();//调用继承来的父类的clone()方法
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
public class Main{
public static void main(String[] args) {
Resume a=new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b=a.clone();
b.setWorkExperience("1998-2006", "YY企业");
Resume c=a.clone();
c.setPersonalInfo("男", "24");
a.Display();
b.Display();
c.Display();
}
}
实现了clone()方法来克隆.
三、浅复制与深复制
加入了一个工作经历类
//加入一个工作经历类
class WorkExperience{
private String workDate;
private String company;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience work=new WorkExperience();
public Resume(String name) {
this.name=name;
}
public void setPersonalInfo(String sex,String age) {//设置个人信息
this.sex=sex;
this.age=age;
}
public void setWorkExperience(String workDate,String company) {//设置工作经历
work.setWorkDate(workDate);
work.setCompany(company);
}
public void Display() {
System.out.println(name+" "+sex+" "+age);
System.out.println("工作经历:"+work.getWorkDate()+" "+work.getCompany());
}
public Resume clone() {
try {
return (Resume)super.clone();//调用继承来的父类的clone()方法
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
public class Main{
public static void main(String[] args) {
Resume a=new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b=a.clone();
b.setWorkExperience("1998-2006", "YY企业");
Resume c=a.clone();
c.setWorkExperience("1998-2003", "ZZ企业");;
a.Display();
b.Display();
c.Display();
}
}
运行结果:
大鸟 男 29
工作经历:1998-2003 ZZ企业
大鸟 男 29
工作经历:1998-2003 ZZ企业
大鸟 男 29
工作经历:1998-2003 ZZ企业
三次显示的结果都是最后一次的值。(由于他是浅复制,对引用类型就只复制了引用,对引用的对象还是指向了原来的对象。
简历的深复制实现:
class WorkExperience implements Cloneable{
private String workDate;
private String company;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public WorkExperience clone() {
try {
return (WorkExperience)super.clone();//调用继承来的父类的clone()方法
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience work=new WorkExperience();
public Resume(String name) {
this.name=name;
}
private Resume(WorkExperience work) {//提供clone()方法调用的私有构造函数,以便克隆工作经历的数据
this.work=(WorkExperience)work.clone();
}
public void setPersonalInfo(String sex,String age) {//设置个人信息
this.sex=sex;
this.age=age;
}
public void setWorkExperience(String workDate,String company) {//设置工作经历
work.setWorkDate(workDate);
work.setCompany(company);
}
public void Display() {
System.out.println(name+" "+sex+" "+age);
System.out.println("工作经历:"+work.getWorkDate()+" "+work.getCompany());
}
public Resume clone() {
Resume obj=new Resume(name);
obj.sex=this.sex;
obj.age=this.age;
obj.work=work.clone();
return obj;
}
}
public class Main{
public static void main(String[] args) {
Resume a=new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b=a.clone();
b.setWorkExperience("1998-2006", "YY企业");
Resume c=a.clone();
c.setWorkExperience("1998-2003", "ZZ企业");;
a.Display();
b.Display();
c.Display();
}
}
程序运行结果:
工作经历:1998-2000 XX公司
大鸟 男 29
工作经历:1998-2006 YY企业
大鸟 男 29
工作经历:1998-2003 ZZ企业