java对象的拷贝_java 对象深拷贝通用方法

class Professor0 implements Cloneable {

String name;

int age;

Professor0(String name, int age) {

this.name = name;

this.age = age;

}

public Object clone() throws CloneNotSupportedException {

return super.clone();

}

}

class Student0 implements Cloneable {

String name;// 常量对象。

int age;

Professor0 p;// 学生1和学生2的引用值都是一样的。

Student0(String name, int age, Professor0 p) {

this.name = name;

this.age = age;

this.p = p;

}

public Object clone() {

Student0 o = null;

try {

o = (Student0) super.clone();

} catch (CloneNotSupportedException e) {

System.out.println(e.toString());

}

return o;

}

}

public class ShallowCopy {

public static void main(String[] args) {

Professor0 p = new Professor0("wangwu", 50);

Student0 s1 = new Student0("zhangsan", 18, p);

Student0 s2 = (Student0) s1.clone();

s2.p.name = "lisi";

s2.p.age = 30;

s2.name = "z";

s2.age = 45;

System.out.println("学生s1的姓名:" + s1.name + "\n学生s1教授的姓名:" + s1.p.name + "," + "\n学生s1教授的年纪" + s1.p.age);// 学生1的教授

}

}

s2变了,但s1也变了,证明s1的p和s2的p指向的是同一个对象。这在我们有的实际需求中,却不是这样,因而我们需要深拷贝:

class Professor implements Cloneable {

String name;

int age;

Professor(String name, int age) {

this.name = name;

this.age = age;

}

public Object clone() {

Object o = null;

try {

o = super.clone();

} catch (CloneNotSupportedException e) {

System.out.println(e.toString());

}

return o;

}

}

class Student implements Cloneable {

String name;

int age;

Professor p;

Student(String name, int age, Professor p) {

this.name = name;

this.age = age;

this.p = p;

}

public Object clone() {

Student o = null;

try {

o = (Student) super.clone();

} catch (CloneNotSupportedException e) {

System.out.println(e.toString());

}

o.p = (Professor) p.clone();

return o;

}

}

public class DeepCopy {

public static void main(String args[]) {

long t1 = System.currentTimeMillis();

Professor p = new Professor("wangwu", 50);

Student s1 = new Student("zhangsan", 18, p);

Student s2 = (Student) s1.clone();

s2.p.name = "lisi";

s2.p.age = 30;

System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age);// 学生1的教授不改变。

long t2 = System.currentTimeMillis();

System.out.println(t2-t1);

}

}

①为什么我们在派生类中覆盖Object的clone()方法时,一定要调用super.clone()呢?在运行时刻,Object中的 clone()识别出你要复制的是哪一个对象,然后为此对象分配空间,并进行对象的复制,将原始对象的内容一一复制到新对象的存储空间中。

②继承自java.lang.Object类的clone()方法是浅复制。以下代码可以证明之。

当然我们还有一种深拷贝方法,就是将对象串行化:

import java.io.*;

//Serialization is time-consuming

class Professor2 implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

String name;

int age;

Professor2(String name, int age) {

this.name = name;

this.age = age;

}

}

class Student2 implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

String name;// 常量对象。

int age;

Professor2 p;// 学生1和学生2的引用值都是一样的。

Student2(String name, int age, Professor2 p) {

this.name = name;

this.age = age;

this.p = p;

}

public Object deepClone() throws IOException, OptionalDataException,

ClassNotFoundException {

// 将对象写到流里

ByteArrayOutputStream bo = new ByteArrayOutputStream();

ObjectOutputStream oo = new ObjectOutputStream(bo);

oo.writeObject(this);

// 从流里读出来

ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());

ObjectInputStream oi = new ObjectInputStream(bi);

return (oi.readObject());

}

}

public class DeepCopy2 {

/**

* @param args

*/

public static void main(String[] args) throws OptionalDataException,

IOException, ClassNotFoundException {

long t1 = System.currentTimeMillis();

Professor2 p = new Professor2("wangwu", 50);

Student2 s1 = new Student2("zhangsan", 18, p);

Student2 s2 = (Student2) s1.deepClone();

s2.p.name = "lisi";

s2.p.age = 30;

System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age); // 学生1的教授不改变。

long t2 = System.currentTimeMillis();

System.out.println(t2-t1);

}

}

但是串行化却很耗时,在一些框架中,我们便可以感受到,它们往往将对象进行串行化后进行传递,耗时较多。

3.利用串行化来做深复制

把对象写到流里的过程是串行化(Serilization)过程,但是在Java程序师圈子里又非常形象地称为“冷冻”或者“腌咸菜(picking)”过程;而把对象从流中读出来的并行化(Deserialization)过程则叫做“解冻”或者“回鲜(depicking)”过程。应当指出的是,写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面,因此“腌成咸菜”的只是对象的一个拷贝,Java咸菜还可以回鲜。

在Java语言里深复制一个对象,常常可以先使对象实现Serializable接口,然后把对象(实际上只是对象的一个拷贝)写到一个流里(腌成咸菜),再从流里读出来(把咸菜回鲜),便可以重建对象。

如下为深复制源代码。

public Object deepClone()

{

//将对象写到流里

ByteArrayOutoutStream bo=new ByteArrayOutputStream();

ObjectOutputStream oo=new ObjectOutputStream(bo);

oo.writeObject(this);

//从流里读出来

ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());

ObjectInputStream oi=new ObjectInputStream(bi);

return(oi.readObject());

}

这样做的前提是对象以及对象内部所有引用到的对象都是可串行化的,否则,就需要仔细考察那些不可串行化的对象可否设成transient,从而将之排除在复制过程之外。

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-12-17 14:02

浏览 4221

评论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值