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: [email protected]
 * 
 * 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]