大话设计模式 原形模型

package org.ustc.prototype;

public class Resume implements Cloneable {
	private String name;
	private String gender;
	private int age;
	private Birthday birth;
	

	public Birthday getBirth() {
		return birth;
	}

	public void setBirth(Birthday birth) {
		this.birth = birth;
	}

	public String getName() {
		return name;
	}

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

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

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

	@Override
protected Resume clone() {
	// TODO Auto-generated method stub
	Resume clone =null;
	
		try {
		clone = (Resume) super.clone();
	} catch (CloneNotSupportedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	clone.birth = (Birthday) this.birth.clone();//生拷贝 钱拷贝
	return clone;
}

	@Override
	public String toString() {
		return "Resume [name=" + name + ", gender=" + gender + ", age=" + age
				+ ", birth=" + birth + "]";
	}
}
package org.ustc.prototype;

public class Birthday implements Cloneable{
	private int year;
	private int month;
	private int day;
	public Birthday(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	@Override
	public String toString() {
		return "Birthday [year=" + year + ", month=" + month + ", day=" + day
				+ "]";
	}
	@Override
	protected Object clone() {
		// TODO Auto-generated method stub
		Object clone = null;
		try{
			clone = super.clone();
		}catch(CloneNotSupportedException e){
			e.printStackTrace();
		}
		return clone;
	}
	
}
package org.ustc.prototype;

public class Main {
public static void main(String[] args) {
	Resume res = new Resume();
	res.setAge(30);
	res.setGender("male");
	res.setName("大鸟");
	Birthday birth = new Birthday(1999,1,1);
	res.setBirth(birth);
	
	Resume res1 = (Resume) res.clone();
	res1.getBirth().setDay(50);
	//res1.setBirth(new Birthday(1928, 2, 2));
	//res1.setBirth(birth);
	System.out.println(res1);
	System.out.println(res);
}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值