从最初代码到 原型模式(以简历类为例)

简历代码初步实现

简历

public class Resume {

	private String name;
	private String age;
	private String sex;
	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.getName()+" "+this.getSex()+" "+this.getAge());
		System.out.println("工作经历 "+this.getTimeArea()+" "+this.getCompany());
	}
	//省略get ,set方法
	}
**客户端调用代码**
	public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		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();
	}
}

b、c可以更改为{
Resume b=a;
,。。。
}
结果
在这里插入图片描述

简历的原型实现

简历类

package 简历类;

public class Resume2 implements Cloneable{

	private String name;
	private String age;
	private String sex;
	private String timeArea;
	private String company;
	
	public Resume2(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 String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getTimeArea() {
		return timeArea;
	}

	public void setTimeArea(String timeArea) {
		this.timeArea = timeArea;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
		
		
	}
	
	//显示
		public void DIsplay() {
			System.out.println(this.getName()+" "+this.getSex()+" "+this.getAge());
			System.out.println("工作经历 "+this.getTimeArea()+" "+this.getCompany());
		}
		public Object Clone() {
			try {
				return (Object)this.clone();
			} catch (CloneNotSupportedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			return null;
		}
}

客户端

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		Resume2 a=new Resume2("大鸟");
		a.SetPersonalInfo("男","29");
		a.SetWorkExperience("1998-2000", "xx公司");
		
		Resume2 b=(Resume2) a.Clone();
		b.SetWorkExperience("1998-2006", "yy公司");
		
		Resume2 c=(Resume2) a.Clone();
		c.SetPersonalInfo("男","24");
		
		a.DIsplay();
		b.DIsplay();
		c.DIsplay();
	}

}

结果
在这里插入图片描述

浅复制

工作经历类

package 简历类;

public 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;
	}
}

简历类

package 简历类;

public class Resume3 implements Cloneable{



		private String name;
		private String age;
		private String sex;

		private WorkExperience work;
		
		public Resume3(String name) {
			this.name=name;
			work=new WorkExperience();
		}
		
		//设置个人信息
		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 String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getAge() {
			return age;
		}

		public void setAge(String age) {
			this.age = age;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}
		
		//显示
			public void DIsplay() {
				System.out.println(this.getName()+" "+this.getSex()+" "+this.getAge());
				System.out.println("工作经历 "+work.getWorkDate()+" "+work.getCompany());
			}
			public Object Clone() {
				try {
					return (Object)super.clone();
				} catch (CloneNotSupportedException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				return null;
			}
	}


客户端

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		Resume3 a=new Resume3("大鸟");
		a.SetPersonalInfo("男","29");
		a.SetWorkExperience("1998-2000", "xx公司");
		
		Resume3 b=(Resume3) a.Clone();
		b.SetWorkExperience("1998-2006", "yy公司");
		
		Resume3 c=(Resume3) a.Clone();
		c.SetPersonalInfo("男","24");
		c.SetWorkExperience("1998-2003", "zz公司");
		
		a.DIsplay();
    	b.DIsplay();
		c.DIsplay();
	}

}

结果
在这里插入图片描述

简历的深复制实现

工作经历类

package 简历类;

public class WorkExperience2 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 Object Clone() {
		try {
			return (Object)this.clone();
		} catch (CloneNotSupportedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return company;
	}
}

简历类

package 简历类;

public class Resume4 implements Cloneable{



		private String name;
		private String age;
		private String sex;

		private WorkExperience2 work;
		
		public Resume4(String name) {
			this.name=name;
			work=new WorkExperience2();
		}
		
		 private Resume4(WorkExperience2 work) {
			this.work=(WorkExperience2)work.Clone();
		}
		
		//设置个人信息
		public void SetPersonalInfo(String sex,String age) {
			this.sex=sex;
			this.age=age;
		}
		
//设置工作经历
		public void SetWorkExperience2(String workDate,String company) {
			work.setWorkDate(workDate);
			work.setCompany(company);
		}
		
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getAge() {
			return age;
		}

		public void setAge(String age) {
			this.age = age;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}
		
		//显示
			public void DIsplay() {
				System.out.println(this.getName()+" "+this.getSex()+" "+this.getAge());
				System.out.println("工作经历 "+work.getWorkDate()+" "+work.getCompany());
			}
			public Object Clone() {
				Resume4 obj=new Resume4(this.work);
				obj.name=this.name;
				obj.sex=this.sex;
				obj.age=this.age;
				return obj;
			}
	}


客户端

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		Resume4 a=new Resume4("大鸟");
		a.SetPersonalInfo("男","29");
		a.SetWorkExperience2("1998-2000", "xx公司");
		
		Resume4 b=(Resume4) a.Clone();
		b.SetWorkExperience2("1998-2006", "yy公司");
		
		Resume4 c=(Resume4) a.Clone();
		c.SetPersonalInfo("男","24");
		c.SetWorkExperience2("1998-2003", "zz公司");
		
		a.DIsplay();
    	b.DIsplay();
		c.DIsplay();
	}

}

结果
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值