原型模式

原:
import java.util.*;
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.age=age;this.sex=sex;
    }
    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 Java1{
	public static void main(String[] args) {
		Resume a=new Resume("大鸟");
		a.setpersonalInfo("男", "29");
		a.setWorkExperience("1988-2000", "xx公司");
		Resume b=new Resume("大鸟");
		b.setpersonalInfo("男", "29");
		b.setWorkExperience("1988-2000", "xx公司");
		Resume c=new Resume("大鸟");
		c.setpersonalInfo("男", "29");
		c.setWorkExperience("1988-2000", "xx公司");
		a.display(); b.display(); c.display();
	}
}
改:
import java.util.*;
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.age=age;this.sex=sex;
    }
    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();
    	}
    	catch (CloneNotSupportedException e)
    	{
    		e.printStackTrace();
    		return null;
    	}
    	
    }
}
public class Java1{
	public static void main(String[] args) {
		Resume a=new Resume("大鸟");
		a.setpersonalInfo("男", "29");
		a.setWorkExperience("1988-2000", "xx公司");
		
		Resume b=a.clone();
		b.setWorkExperience("1952-2000", "yy");
		
		Resume c=a.clone();
		c.setpersonalInfo("男","24");
		
		a.display(); b.display(); c.display();
	}
}
原,浅复制:
import java.util.*;
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.age=age;this.sex=sex;
    }
    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.getCompany()+"   "+work.getWorkDate());
    }
    public Resume clone() throws CloneNotSupportedException
    {
    		return (Resume) super.clone();
    }
}
public class Java1{
	public static void main(String[] args) throws CloneNotSupportedException {
		Resume a=new Resume("大鸟");
		a.setpersonalInfo("男", "29");
		a.setWorkExperience("1988-2000", "xx公司");
		
		Resume b=a.clone();
		b.setWorkExperience("1952-2000", "yy");
		
		Resume c=a.clone();
		c.setWorkExperience("男","24");
		
		a.display(); b.display(); c.display();
	}
}
改,深复制:
import java.util.*;
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();
    	}
    	catch (CloneNotSupportedException e)
    	{
    		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;
        }
    public void setpersonalInfo( String sex,String age)
    {
    	this.age=age;this.sex=sex;
    }
    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.getCompany()+"   "+work.getWorkDate());
    }
    public Resume clone() 
    {
    		Resume obj =new Resume (name);
    		obj.age=this.age;
    		obj.sex=this.sex;
    		obj.work=work.clone();
    		
    	return obj;
    }
}
public class Java1{
	public static void main(String[] args) throws CloneNotSupportedException {
		Resume a=new Resume("大鸟");
		a.setpersonalInfo("男", "29");
		a.setWorkExperience("1988-2000", "xx公司");
		
		Resume b=a.clone();
		b.setWorkExperience("1952-2000", "yy");
		
		Resume c=a.clone();
		c.setWorkExperience("1922-2629","sdscs");
		
		a.display(); b.display(); c.display();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值