设计模式之原型模式1,prototype

package liu.prototype.demo;
public class WorkExperience {
 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;
 }
}

----------------------------------------
package liu.prototype.demo;
public 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();
 }
 
 public void setPersonalInfo(String sex, String age){
  this.age = age;
  this.sex = sex;
 }
 
 public void setWorkExperience(String workDate, String company){
  work.setCompany(company);
  work.setWorkDate(workDate);
 }
 
 public void display(){
  System.out.println("姓名:"+name);
  System.out.println("年龄:"+age);
  System.out.println("性别:"+sex);
  System.out.println("工作时间:"+work.getWorkDate());
  System.out.println("公司:"+work.getCompany());
 }
 
 public Object clone(){
  Object object = null;
  try {
   object = super.clone();
  } catch (CloneNotSupportedException e) {
   System.err.println("AbstractSpoon is not Cloneable");
   e.printStackTrace();
  }
  return object;
  
 }
}

 ----------------------------------------
package liu.prototype.demo;
public class Main {

 public static void main(String[] args) {
  Resume a = new Resume("大鸟");
  a.setPersonalInfo("male", "29");
  a.setWorkExperience("2009", "xx company");
  
  Resume b = (Resume)a.clone();
  b.setWorkExperience("2009", "yy company");
  Resume c = (Resume) a.clone();
  c.setPersonalInfo("male", "24");
  c.setWorkExperience("2008", "zz company");
  
  a.display();
  b.display();
  c.display();
 }
}

 ----------------------------------------

 ----------------------------------------

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值