赞
踩
- package test01;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
-
- public class Test01 {
-
- public static void main(String[] args) {
- fun();
- }
- public static void fun() {
- Face face = new Face(10);
- Head head = new Head(face,20);
- Person p = new Person("张小三",18,head);
-
- Person p1 = (Person)p.clone(); //进行clone
-
- //修改克隆对象的属性值,以判断原始对象和克隆对象是否共享内部对象head和内部的内部对象face
- p1.setAge(28);
- p1.getHead().setH(22);
- p1.getHead().getFace().setF(11);
-
- System.out.println(p);
- System.out.println(p1);
-
- System.out.println("p.head == p1.head:"+(p.getHead() == p1.getHead()));
- System.out.println("p.head.face == p1.head.face:"+(p.getHead().getFace() == p1.getHead().getFace()));
- }
-
- }
-
- class SerialCloneable implements Cloneable,Serializable {
- public Object clone() {
- try {
- //save the object to a byte array
- //字节数组输出流会自动创建字节数组
- ByteArrayOutputStream bout = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(bout);
- out.writeObject(this);
- out.close();
-
- //read a clone of the object from the byte array
- //字节数组输入流不会创建字节数组
- ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
- ObjectInputStream in = new ObjectInputStream(bin);
- Object ret = in.readObject();
- in.close();
- return ret;
- } catch (Exception e) {
- return null;
- }
- }
- }
-
- class Person extends SerialCloneable {
- private String name;
- private int age;
- private Head head;
- public Person() {
- super();
- }
- public Person(String name, int age, Head head) {
- super();
- this.name = name;
- this.age = age;
- this.head = head;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Head getHead() {
- return head;
- }
- public void setHead(Head head) {
- this.head = head;
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + ", head=" + head + "]";
- }
- }
-
- /*将 implements Cloneable,Serializable 改为 extends SerialCloneable 也可以*/
- class Head implements Serializable {
- private Face face;
- private int h; //用于验证Head对象的空间是否被共享
- public Head() {
- super();
- }
- public Head(Face face, int h) {
- super();
- this.face = face;
- this.h = h;
- }
- public Face getFace() {
- return face;
- }
- public void setFace(Face face) {
- this.face = face;
- }
- public int getH() {
- return h;
- }
- public void setH(int h) {
- this.h = h;
- }
- @Override
- public String toString() {
- return "Head [face=" + face + ", h=" + h + "]";
- }
- }
-
- /*将 implements Cloneable,Serializable 改为 extends SerialCloneable 也可以*/
- class Face implements Serializable {
- private String str = "帅气的脸蛋";
- private int f ; //用于验证Face对象的空间是否被共享
- public Face() {
- super();
- }
- public Face(int f) {
- super();
- this.str = str;
- this.f = f;
- }
- public int getF() {
- return f;
- }
- public void setF(int f) {
- this.f = f;
- }
- @Override
- public String toString() {
- return "Face [str=" + str + ", f=" + f + "]";
- }
- }
- //运行结果:
- Person [name=张小三, age=18, head=Head [face=Face [str=帅气的脸蛋, f=10], h=20]]
- Person [name=张小三, age=28, head=Head [face=Face [str=帅气的脸蛋, f=11], h=22]]
- p.head == p1.head:false
- p.head.face == p1.head.face:false
运行结果可以看出,这是一个彻底的深拷贝。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。