zhb8015

posts(23) comments(6) trackbacks(0)
  • BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • hadoop

随笔档案

  • 2013年3月 (1)
  • 2012年10月 (2)
  • 2012年8月 (2)
  • 2012年7月 (1)
  • 2012年6月 (1)
  • 2012年5月 (1)
  • 2012年4月 (5)

文章分类

  • arithmetc
  • books(2)
  • design patter(4)
  • English(1)
  • exception(3)
  • hadoop(1)
  • interview(53)
  • Kent Beck
  • linux,unix(1)
  • MartinFlow(7)
  • method(7)
  • middleware(1)
  • projectManagement(6)
  • soa(9)
  • ssh(14)
  • ThoughtWork(2)
  • tibco(13)

文章档案

  • 2013年4月 (1)
  • 2013年3月 (3)
  • 2012年8月 (1)
  • 2012年7月 (8)
  • 2012年6月 (15)
  • 2012年5月 (14)
  • 2012年4月 (22)
  • 2012年3月 (5)

相册

  • java

搜索

  •  

最新评论

  • 1. re: Log4j详细配置(转)
  • 写得很详细,最后那句好像有点小问题,输出到test1和stdout应该是log4j.logger.myTest1=DEBUG, test1, stdout ?
  • --aramxiao
  • 2. re: 结合Maven2进行J2EE项目构建(转)
  • 评论内容较长,点击标题查看
  • --最代码
  • 3. re: java深浅复制
  • 评论内容较长,点击标题查看
  • --zhb8015
  • 4. re: 求质数,难以理解的代码,有兴趣可以看一下
  • 评论内容较长,点击标题查看
  • --zhb8015
  • 5. re: Advice about migrating to new platfrom
  • platfrom or platform??
  • --qingyue

阅读排行榜

评论排行榜

View Post

java深浅复制

一、概念
二、实现方式及简单原理 
三、实例
四、参考
五、单元测试
六、代码下载
  (deepCopy.rar)

一、概念

A、浅复制:浅复制仅仅复制所考虑的对象,而不复制它所引用的对象

被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。

B、深复制:深复制把要复制的对象所引用的对象都复制了一遍

被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。

二、实现方式及原理

1、浅复制:实现Cloneable接口,重写clone()方法()

2、深复制:实现serializable接口,字节流转化为对象流来处理

三、实例 

A、浅复制

public class Student implements Cloneable {

public String studentName;

public int studentAge;

public Student(String studentName, int studentAge) {

this.studentName = studentName;

this.studentAge = studentAge;

}

@Override

public Student clone() throws CloneNotSupportedException {

return (Student)super.clone();

}

}

B1、深复制(Cloneable)    B2、深复制(Serializable)

public class Teacher implements Cloneable,Serializable {

String teacherName;

int teacherAge;

public Teacher(String teacherName, int teacherAge) {

this.teacherName = teacherName;

this.teacherAge = teacherAge;

}

@Override

protected Object clone() throws CloneNotSupportedException {

return super.clone();

}

}

public class Man implements Cloneable,Serializable {

String name;

int age;

public Teacher teacher;

public Man(String name, int age, Teacher teacher) {

this.name = name;

this.age = age;

this.teacher = teacher;

}

@Override

public Object clone() throws CloneNotSupportedException {

Man man = null;

man = (Man)super.clone();

man.teacher = (Teacher) teacher.clone();   //important

return man;

}

/**

 * serialized copy  

 * @return Man

 * @throws IOException

 * @throws ClassNotFoundException

 */

public Object serialClone() throws IOException, ClassNotFoundException {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(baos);

oos.writeObject(this);

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

ObjectInputStream ois = new ObjectInputStream(bais);

return ois.readObject();

}

}

四、参考:

(经典)http://blog.csdn.net/guitacom/article/details/665055#

http://dev.firnow.com/course/3_program/java/javashl/2008510/115047.html

五、单元测试代码

public class StudentTest {

@BeforeClass

public static void setUpBeforeClass() throws Exception {

}

@Test

public void testShallowClone() throws Exception {

String name = "Robert C. Martin";

int age = 40;

Student student = new Student(name, age);

Student studentCopy = (Student) student.clone();

studentCopy.studentName = "Kent Benk";

studentCopy.studentAge = 55;

assertEquals(student.studentName, "Robert C. Martin");

assertEquals(student.studentAge, 40);

}

@Test

public void testDeepClone() throws CloneNotSupportedException {

String manName = "Robert C. Martin";

int manAge = 40;

String teacherName = "T1";

int teacherAge = 50;

Teacher teacher = new Teacher(teacherName, teacherAge);

Man man = new Man(manName, manAge, teacher);

Man replication = (Man)man.clone();

replication.teacher.teacherName = "T2";

replication.teacher.teacherAge = 60;

assertEquals(man.teacher.teacherName, "T1"); //original teacher's properties don't change

assertEquals(man.teacher.teacherAge, 50);

}

@Test

public void testSerialDeepClone() throws IOException, ClassNotFoundException {

String manName = "Robert C. Martin";

int manAge = 40;

int teacherAge = 50;

String teacherName = "T1";

Teacher teacher = new Teacher(teacherName, teacherAge);

Man man = new Man(manName, manAge, teacher);

Man replication = (Man) man.serialClone();

replication.teacher.teacherName = "T2";

replication.teacher.teacherAge = 60;

assertEquals(man.teacher.teacherName, "T1"); //original teacher's properties don't change

assertEquals(man.teacher.teacherAge, 50);

}


posted on 2012-04-13 14:47 zhb8015 阅读(667) 评论(1)  编辑  收藏 所属分类: interview

View Comments

# re: java深浅复制  回复  更多评论   
补充一下:
七、性能测试:
测试结果:复制100000 克隆复制是串行化复制的74倍
100000 clone time:47
100000 serial clone time:3500
Multiple is(serial/clone):74

测试单元的代码:
******************
@Test
public void deepCloneVsSerial() throws CloneNotSupportedException, IOException, ClassNotFoundException {
int max = 100000;

String manName = "Robert C. Martin";
int manAge = 40;

int teacherAge = 50;
String teacherName = "T1";
Teacher teacher = new Teacher(teacherName, teacherAge);

Man man = new Man(manName, manAge, teacher);

//begin
long start1 = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
man.clone();
}
long end1 = System.currentTimeMillis();
System.err.println(max + " clone time:" + (end1 - start1));

long start2 = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
man.serialClone();
}
long end2 = System.currentTimeMillis();
System.err.println(max + " serial clone time:" + (end2 - start2));

System.err.println("Multiple is(serial/clone):" + (end2 - start2)/(end1 - start1));
/**
* 100000 clone time:47
100000 serial clone time:3438
Multiple is(serial/clone):73
*/
}

******************
2012-04-13 15:24 | zhb8015
新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
相关文章:
  • Nutch vs Lucene(转)
  • ArrayList空间增长是怎么样的
  • 手工打包(jar)
  • TW interview experience(转)
  • 排序算法(转)
  • 加密算法(转)
  • 软件设计过程一些术语 AN BD FD DD CD CT
  • Log4j详细配置(转)
  • log4j详解
  • 时间复杂度
 
 
Powered by:
BlogJava
Copyright © zhb8015