0
点赞
收藏
分享

微信扫一扫

浅谈java 对象克隆


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 条评论