博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java的浅克隆_java浅克隆与深克隆
阅读量:6453 次
发布时间:2019-06-23

本文共 4251 字,大约阅读时间需要 14 分钟。

浅克隆 通常只是对克隆的实例进行复制,但里面的其他子对象,都是共用的。

深克隆 克隆的时候会克隆它的子对象的引用,里面所有的变量和子对象都是又额外拷贝了一份。

浅克隆

/**

* 浅克隆

* @author InJavaWeTrust

*

*/

class Father implements Cloneable {

public String name;

public int age;

public Father(String name, int age) {

this.name = name;

this.age = age;

}

}

public class Child implements Cloneable {

public String name;

public int age;

public Father father;

public Child(String name, int age, Father father) {

this.name = name;

this.age = age;

this.father = father;

}

public Object clone() {

Child child = null;

try {

child = (Child) super.clone();

} catch (CloneNotSupportedException e) {

e.printStackTrace();

}

return child;

}

public static void main(String[] args) {

Father father = new Father("李刚", 44);

Child child1 = new Child("李启铭", 14, father);

Child child2 = (Child) child1.clone();

child2.father.name = "赵刚";

child2.father.age = 40;

child2.name = "赵启铭";

child2.age = 15;

System.out.println(child1.name + " " + child1.age);

System.out.println(child1.father.name + " " + child1.father.age);

System.out.println(child2.name + " " + child2.age);

System.out.println(child2.father.name + " " + child2.father.age);

}

//运行结果

//李启铭 14

//赵刚 40

//赵启铭 15

//赵刚 40

//浅克隆时father对象都是共用的,所以father重新赋值之后,之前的值也跟着变化。

}

深克隆

/**

* 深克隆

* @author InJavaWeTrust

*

*/

class Father implements Cloneable {

public String name;

public int age;

public Father(String name, int age) {

this.name = name;

this.age = age;

}

public Object clone() {

Father father = null;

try {

father = (Father) super.clone();

} catch (CloneNotSupportedException e) {

e.printStackTrace();

}

return father;

}

}

public class Child implements Cloneable {

private String name;

private int age;

private Father father;

public Child(String name, int age, Father father) {

this.name = name;

this.age = age;

this.father = father;

}

public Object clone() {

Child child = null;

try {

child = (Child) super.clone();

child.father = (Father) father.clone();

} catch (CloneNotSupportedException e) {

e.printStackTrace();

}

return child;

}

public static void main(String[] args) {

Father father = new Father("李刚", 44);

Child child1 = new Child("李启铭", 14, father);

Child child2 = (Child) child1.clone();

child2.father.name = "赵刚";

child2.father.age = 40;

child2.name = "赵启铭";

child2.age = 15;

System.out.println(child1.name + " " + child1.age);

System.out.println(child1.father.name + " " + child1.father.age);

System.out.println(child2.name + " " + child2.age);

System.out.println(child2.father.name + " " + child2.father.age);

}

//运行结果

//李启铭 14

//李刚 44

//赵启铭 15

//赵刚 40

//深克隆时father对象也 重新拷贝了一份,所以father重新赋值之后,之前的值不会发生变化。

}

利用序列化也可以实现深克隆。

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

/**

* 序列化深克隆

* @author InJavaWeTrust

*

*/

class Father implements Serializable {

private static final long serialVersionUID = 5339188002945518918L;

public String name;

public int age;

public Father(String name, int age) {

this.name = name;

this.age = age;

}

}

public class Child implements Serializable{

private static final long serialVersionUID = 991407861345394819L;

private String name;

private int age;

private Father father;

public Child(String name, int age, Father father) {

this.name = name;

this.age = age;

this.father = father;

}

public Object deepClone() {

Object object = null;

try {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(baos);

oos.writeObject(this);

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

ObjectInputStream ois = new ObjectInputStream(bais);

object = ois.readObject();

} catch (Exception e) {

e.printStackTrace();

}

return object;

}

public static void main(String[] args) {

Father father = new Father("李刚", 44);

Child child1 = new Child("李启铭", 14, father);

Child child2 = (Child) child1.deepClone();

child2.father.name = "赵刚";

child2.father.age = 40;

child2.name = "赵启铭";

child2.age = 15;

System.out.println(child1.name + " " + child1.age);

System.out.println(child1.father.name + " " + child1.father.age);

System.out.println(child2.name + " " + child2.age);

System.out.println(child2.father.name + " " + child2.father.age);

}

//运行结果

//李启铭 14

//李刚 44

//赵启铭 15

//赵刚 40

}

转载地址:http://fcyzo.baihongyu.com/

你可能感兴趣的文章
Java三大特征之继承(二)
查看>>
python装饰器
查看>>
dubbo-zookeeper(续)
查看>>
(转)C#委托的介绍(delegate、Action、Func、predicate)
查看>>
PyCharm Tips 常用操作帮助
查看>>
IOS App 实现通过URL 超链接进行跳转
查看>>
CSS font-family 属性
查看>>
阿里Java完整学习资料
查看>>
建立本地repo 管理仓库
查看>>
.Net转Java自学之路—基础巩固篇十(异常)
查看>>
详解redis服务
查看>>
数据结构之--单链表MyArrayList
查看>>
java基础练习2
查看>>
文件操作总结
查看>>
两队选手每队5人进行一对一的比赛(算法)
查看>>
eclipse : Error while performing database login with the driver null
查看>>
【语法】数组Array
查看>>
WebBrowser一点心得,如果在Javascript和Winform代码之间实现双向通信
查看>>
vue elementUI之Form表单 验证
查看>>
Android程序完全退出的三种方法
查看>>