`
这些年
  • 浏览: 388453 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

对象的深度复制

    博客分类:
  • java
 
阅读更多

1:使用序列化

      1)被序列化的对象实现Serializable接口(arraylist实现了这个接口)

       

package com.chinachche.datacorrection.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class DataUtil {
	/**
	 * 深度复制
	 */
	public static Serializable deeplyCopy(Serializable src) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(src);
			oos.close();
			baos.close();

			byte[] data = baos.toByteArray();
			ByteArrayInputStream bais = new ByteArrayInputStream(data);
			ObjectInputStream ois = new ObjectInputStream(bais);
			Serializable copy = (Serializable) ois.readObject();
			ois.close();
			bais.close();
			return copy;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

 

java.io.StreamCorruptedException: invalid stream header: EFBFBDEF

http://tristan1.iteye.com/blog/752895

http://blog.csdn.net/wangwenjunsw/article/details/5550441

 

2:克隆(复杂问题解决困难)

       1). 在类的声明中加入“ implements Cloneable ”,标志该类有克隆功能;
       2). 重载类 Object 的 clone() 方法,在该方法中调用 super.clone() :

       3)类中包含其它对象的,其它对象的类也须要声明为Cloneable

 

package com.chinachche.datacorrection.util;

public class cloneBeanA implements Cloneable {//实现接口
	private String userName;
	private Friend friend;       //对象类型变量

	public Object clone() {
		try {
			cloneBeanA b = (cloneBeanA) super.clone();
			b.friend = (Friend) friend.clone();//克隆对象类型数据
			return b;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Friend getFriend() {
		return friend;
	}

	public void setFriend(Friend friend) {
		this.friend = friend;
	}
}

 

package com.chinachche.datacorrection.util;

public class Friend implements Cloneable {//实现接口
	private String friendName;

	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getFriendName() {
		return friendName;
	}

	public void setFriendName(String friendName) {
		this.friendName = friendName;
	}
}

 

 测试代码:

package com.chinachche.datacorrection.util;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class testClone {
	Friend f1 = new Friend();

	@Before
	public void Init() {
		f1.setFriendName("zhang san");
	}

	@Test
	public void testClones() {
		cloneBeanA a = new cloneBeanA();
		a.setUserName("gbz");
		a.setFriend(f1);

		cloneBeanA b = (cloneBeanA) a.clone();
		b.setUserName("123");
		f1.setFriendName("456");

		assertEquals("456", a.getFriend().getFriendName());
		assertEquals("zhang san", b.getFriend().getFriendName());
	}
}

 关于克隆的详解:http://www.iteye.com/topic/428920

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics