您的位置:首页 > 其它

设计模式之原型模式

2010-05-18 21:32 260 查看
public class PrototypeTest1
{
	public static void main(String[] args)
		throws CloneNotSupportedException
	{
		Resume a = new Resume("大鸟");
		a.SetPersonalInfo("男","29");
		a.SetWorkExpeience("1998-2000","XX公司");
		Resume b = (Resume)a.Clone();
		b.SetWorkExpeience("1998-2006","YY公司");
		Resume c = (Resume)a.Clone();
		c.SetWorkExpeience("1998-2003","ZZ公司");
		a.Display();
		b.Display();
		c.Display();
		System.out.println();
	}
}
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 SetPersonalInfo(String sex,String age)
	{
		this.sex = sex;
		this.age = age;
	}
	public void SetWorkExpeience(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 Object Clone()throws CloneNotSupportedException
	{
		Resume obj = new Resume(this.work);
		obj.name = this.name;
		obj.sex = this.sex;
		obj.age = this.age;
		return obj;
	}
}
class WorkExperience implements Cloneable
{
	private String workDate;
	private String company;
	public void setworkDate(String workDate)
	{
		this.workDate = workDate;
	}
	public String getworkDate()
	{
		return workDate;
	}
	public void setcompany(String company)
	{
		this.company = company;
	}
	public String getcompany()
	{
		return company;
	}
	public Object Clone()throws CloneNotSupportedException
	{
		return (Object)this.clone();
	}
}
/*
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 SetWorkExpeience(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 Object Clone()throws CloneNotSupportedException
	{
		return (Object)this.clone();
	}
}*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: