原型克隆模式
来源:互联网 发布:hadoop和java的关系 编辑:程序博客网 时间:2024/06/06 02:48
//浅复制
public class Sheep implements Cloneable,Serializable{
private String sname;private Date birthday;
public Sheep() {}
public Sheep(String sname, Date birthday) {
super();
this.sname = sname;
this.birthday = birthday;
}
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone(); //直接调用object对象的clone()方法!
return obj;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
//深复制
public class Sheep2 implements Cloneable {
private String sname;
private Date birthday;
public Sheep2() {
}
public Sheep2(String sname, Date birthday) {
super();
this.sname = sname;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone(); //直接调用object对象的clone()方法!
//添加如下代码实现深复制(deep Clone)
Sheep2 s = (Sheep2) obj;
s.birthday = (Date) this.birthday.clone(); //把属性也进行克隆!
return obj;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
测试类
Sheep2 s1 = new Sheep2("少利",date);
Sheep2 s2 = (Sheep2) s1.clone();
- 克隆(原型)模式
- 原型克隆模式
- 深度克隆,原型模式
- 从克隆到原型模式
- 原型模式设计克隆人
- 对象的克隆-原型模式
- 原型管理器--对象的克隆--原型模式
- 原型模式(克隆模式,拷贝模式)
- 创建模式之原型模式浅克隆
- 创建模式之原型模式 深克隆
- 【设计模式】对象的克隆-原型模式
- 设计模式之原型模式(深克隆,浅克隆)
- 七、原型模式(深浅克隆)
- 对象的克隆——原型模式
- 对象的克隆——原型模式
- 对象的克隆——原型模式
- 对象的克隆——原型模式
- 对象的克隆--原型模式(Prototype)
- IntelliJ IDEA 使用心得与常用快捷键
- 2.RabbitMq+Spring整合测试
- git push报错 refusing to update checked out branch 解决办法
- C#中string format的使用
- 关于监控方案的一点想法供参考
- 原型克隆模式
- java注解实战
- qt: The build directory needs to be at the same level as the source directory.
- hdu 6253 Knightmare
- 如何让交互稿的体验更好
- 让div标签里面的img标签的图片在任意分辨率下居中显示,两边多余的隐藏掉
- shiro + springMVC + spring + mybatis (maven 整合)
- tcp报文格式与wireshark传输层对应关系
- Smconf 增加新功能啦