原型模式的浅度克隆和深度克隆的实现
来源:互联网 发布:取消淘宝账号实名认证 编辑:程序博客网 时间:2024/06/01 10:36
package com.lovo.prototype;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;
public class Person implements Cloneable,Serializable{
private int id;
private String name;
private int age;
private Date brith;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBrith() {
return brith;
}
public void setBrith(Date brith) {
this.brith = brith;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", brith=" + brith + ", address=" + address + "]";
}
/**
* 浅克隆
*
* @return
* @throws CloneNotSupportedException
*/
public Person myclone()throws CloneNotSupportedException{
return (Person) super.clone();
}
/**
* 深克隆
* @return
* @throws CloneNotSupportedException
* @throws IOException
* @throws ClassNotFoundException
*/
public Person deepclone()throws CloneNotSupportedException, IOException, ClassNotFoundException{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Person p = (Person) ois.readObject();
return p;
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;import java.util.Date;
public class Person implements Cloneable,Serializable{
private int id;
private String name;
private int age;
private Date brith;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBrith() {
return brith;
}
public void setBrith(Date brith) {
this.brith = brith;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", brith=" + brith + ", address=" + address + "]";
}
/**
* 浅克隆
*
* @return
* @throws CloneNotSupportedException
*/
public Person myclone()throws CloneNotSupportedException{
return (Person) super.clone();
}
/**
* 深克隆
* @return
* @throws CloneNotSupportedException
* @throws IOException
* @throws ClassNotFoundException
*/
public Person deepclone()throws CloneNotSupportedException, IOException, ClassNotFoundException{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Person p = (Person) ois.readObject();
return p;
}
}
*********************************** 测试 ***********************************
package com.lovo.prototype;
import java.io.IOException;
import java.util.Date;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Person p = new Person();
p.setId(1);
p.setName("张三");
p.setAge(18);
p.setBrith(new Date());
p.setAddress("四川省成都市武侯区");
Person p2 = p.deepclone();
p2.setName("李四");
p2.setAge(20);
System.out.println(p);
System.out.println("**************************************************");
System.out.println(p2);
}
}
0 0
- 原型模式的浅度克隆和深度克隆的实现
- 原型模式-浅克隆和深克隆的自我理解
- 原型模式的浅克隆和深克隆
- js的浅克隆和深度克隆
- js的浅克隆和深度克隆
- Java的深度克隆和浅度克隆
- Java的浅度克隆和深度克隆
- 深度克隆,原型模式
- 对象的克隆-原型模式
- 对象的克隆——原型模式(三):浅克隆,深克隆
- 对象的克隆——原型模式(三):浅克隆,深克隆
- Clone 方法的使用(深度克隆和浅克隆)
- Java浅度克隆和深度克隆
- js深度克隆和浅度克隆
- 浅度克隆和深度克隆
- 原型管理器--对象的克隆--原型模式
- 【怎样写代码】对象克隆 -- 原型模式(五):原型管理器的引入和实现
- 浅克隆和深度克隆
- HDU 2545 树上战争
- Windows系统Eclipse的copy lines默认快捷键Ctrl+alt+down不可用
- Android入门(54)——第九章 使用GestureOverlayView进行手势识别
- nginx日志文件切割
- pthreads 2.0.10 test
- 原型模式的浅度克隆和深度克隆的实现
- 基于jquery的秒表倒计时
- tarjan题目(1)Victoria的舞会2
- 基于windbg的内存泄露分析
- 在Hibernate Validator 4.1+中,@NotNull, @NotEmpty和@NotBlank之间的区别是什么?
- 单片机内部扩展RAM
- Find Minimum in Rotated Sorted Array II
- 跳转webview白屏(黑屏)问题解决
- 瑞士地理坐标系统简介(Swiss Coordinate System or Swiss Grid)