浅谈java 对象克隆

我们在编码过程经常会碰到将一个对象传递给另一个对象,java中对于基本型变量采用的是值传递,而对于对象比如bean传递时采用的是应用传递也就是地址传递,而很多时候对于对象传递我们也希望能够象值传递一样,使得传递之前和之后有不同的内存地址
package com.mapbar.clone;

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

/**
 * 
 * Class CloneDemo.java
 * 
 * Description
 * 
 * Company mapbar 浅克隆,深度克隆,以及对象序列化克隆
 * 
 * author Chenll E-mail: Chenll@mapbar.com
 * 
 * Version 1.0
 * 
 * Date 2012-4-1 上午09:44:06
 */

// 被克隆的类必须自己实现Cloneable 接口,
// 以指示 Object.clone() 方法可以合法地对该类实例进行按字段复制。
// Cloneable 接口实际上是个标识接口,没有任何接口方法。

class StudentA implements Cloneable {

	@Override
	public String toString() {
		return "StudentA [id=" + id + ", score=" + score + "]";
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public StudentA(String id, double score) {
		super();
		this.id = id;
		this.score = score;
	}

	private String id;

	private double score;

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

}

// 深度克隆对象,当类存在聚合关系的时候,克隆就必须考虑聚合对象的克隆
class StudentB implements Cloneable {

	@Override
	public String toString() {
		return "StudentB [studentA=" + studentA + ", name=" + name + ", score="
				+ score + "]";
	}

	public StudentB(StudentA studentA, String name, double score) {
		super();
		this.studentA = studentA;
		this.name = name;
		this.score = score;
	}

	public StudentA getStudentA() {
		return studentA;
	}

	public void setStudentA(StudentA studentA) {
		this.studentA = studentA;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	private StudentA studentA;

	private String name;

	private double score;

	@Override
	protected Object clone() throws CloneNotSupportedException {
		StudentB sb = (StudentB) super.clone();
		// 克隆其它聚合属性
		if (this.studentA != null) {
			sb.studentA = (StudentA) this.studentA.clone();
		}
		return sb;
	}
}
	
public  class CloneDemo{
	/**
	 * 所谓对象序列化就是将对象的状态转换成字节流,以后可以通过这些值再生成相同状态的对象。 这个过程也可以通过网络实现
	 */
	public static Object copyObject(Object oldObj) {
		Object newObj = null;
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(bo);
			oo.writeObject(oldObj);// 源对象
			ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
			ObjectInputStream oi = new ObjectInputStream(bi);
			newObj = oi.readObject();// 目标对象
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return newObj;
	}
	
	public static void main(String[] args) throws CloneNotSupportedException{
		StudentA a = new StudentA("world",10.0);
		System.out.println("before a:"+a.toString());
		StudentA aa = (StudentA) a.clone();
		a.setId("world20");
		a.setScore(20.0);
		System.out.println("after a:"+a.toString());
		System.out.println("clone a:"+aa.toString());
		
		StudentB b = new StudentB(a,"hello",30);
		System.out.println("before b:"+b.toString());
		StudentB bb = (StudentB) b.clone();
		StudentB bbb = (StudentB) copyObject(b);
		b.getStudentA().setId("helloooo");
		b.setScore(50);
		System.out.println("after b:"+b.toString());
		System.out.println("clone b:"+bb.toString());
		System.out.println("copyObject b:"+bbb.toString());
		
		
	}
}

output:

before a:StudentA [id=world, score=10.0]
after a:StudentA [id=world20, score=20.0]
clone a:StudentA [id=world, score=10.0]
before b:StudentB [studentA=StudentA [id=world20, score=20.0], name=hello, score=30.0]
after b:StudentB [studentA=StudentA [id=helloooo, score=20.0], name=hello, score=50.0]
clone b:StudentB [studentA=StudentA [id=world20, score=20.0], name=hello, score=30.0]
copyObject b:StudentB [studentA=StudentA [id=world20, score=20.0], name=hello, score=30.0]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值