(转)Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
原文地址:http://blog.csdn.net/kenthong/article/details/5758884
Part I
没啥好说的,直接开始Part II吧。
Part II
谈到了对象的克隆,就不得不说为什么要对对象进行克隆。Java中所有的对象都是保存在堆中,而堆是供全局共享的。也就是说,如果同一个Java程序的不同方法,只要能拿到某个对象的引用,引用者就可以随意的修改对象的内部数据(前提是这个对象的内部数据通过get/set方法曝露出来)。有的时候,我们编写的代码想让调用者只获得该对象的一个拷贝(也就是一个内容完全相同的对象,但是在内存中存在两个这样的对象),有什么办法可以做到呢?当然是克隆咯。
Part III
首先,我们是程序员,当然是用我们程序员的语言来交流。
import java.util.Date;
public class User implements Cloneable {
private String username;
private String password;
private Date birthdate;
public User(String username, String password, Date birthdate) {
this.username = username;
this.password = password;
this.birthdate = birthdate;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public int hashCode() {
// 省略equals的实现(可用eclipse自动生成)
}
@Override
public boolean equals(Object obj) {
// 省略equals的实现(可用eclipse自动生成)
}
// 省略一大堆get/set方法
}
上述代码构建了一个User类,并且实现了java.lang.Cloneable接口。顾名思义,Cloneable的意思就是说明这个类可以被克隆的意思。
而我们先去看看java.lang.Cloneable这个接口有些什么。
/*
* @(#)Cloneable.java 1.17 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
/**
* A class implements the Cloneable interface to
* indicate to the {@link java.lang.Object#clone()} method that it
* is legal for that method to make a
* field-for-field copy of instances of that class.
*
* Invoking Object's clone method on an instance that does not implement the
* Cloneable interface results in the exception
* CloneNotSupportedException being thrown.
*
* By convention, classes that implement this interface should override
* Object.clone (which is protected) with a public method.
* See {@link java.lang.Object#clone()} for details on overriding this
* method.
*
* Note that this interface does not contain the clone method.
* Therefore, it is not possible to clone an object merely by virtue of the
* fact that it implements this interface. Even if the clone method is invoked
* reflectively, there is no guarantee that it will succeed.
*
* @author unascribed
* @version 1.17, 11/17/05
* @see java.lang.CloneNotSupportedException
* @see java.lang.Object#clone()
* @since JDK1.0
*/
public interface Cloneable {
}
不要惊讶,没错,除了一大堆的鸡肠以外,这个接口没有定义任何的方法签名。也就是说,我们要克隆一个对象,但是他又不给我提供一个方法。那该怎么办呢?不怕,我们还有全能的Object类,别忘记他可是所有类的始祖啊(神一般的存在着),所以,有事没事都该去问候一下他老人家。
/*
* @(#)Object.java 1.73 06/03/30
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
/**
* Class Object is the root of the class hierarchy.
* Every class has Object as a superclass. All objects,
* including arrays, implement the methods of this class.
*
* @author unascribed
* @version 1.73, 03/30/06
* @see java.lang.Class
* @since JDK1.0
*/
public class Object {
// 省略N多的代码
/**
* Creates and returns a copy of this object. The precise meaning
* of "copy" may depend on the class of the object. The general
* intent is that, for any object x, the expression:
*
*
* x.clone() != x
* will be true, and that the expression:
*
*
* x.clone().getClass() == x.getClass()
* will be true, but these are not absolute requirements.
* While it is typically the case that:
*
*
* x.clone().equals(x)
* will be true, this is not an absolute requirement.
*
* By convention, the returned object should be obtained by calling
* super.clone. If a class and all of its superclasses (except
* Object) obey this convention, it will be the case that
* x.clone().getClass() == x.getClass().
*
* By convention, the object returned by this method should be independent
* of this object (which is being cloned). To achieve this independence,
* it may be necessary to modify one or more fields of the object returned
* by super.clone before returning it. Typically, this means
* copying any mutable objects that comprise the internal "deep structure"
* of the object being cloned and replacing the references to these
* objects with references to the copies. If a class contains only
* primitive fields or references to immutable objects, then it is usually
* the case that no fields in the object returned by super.clone
* need to be modified.
*
* The method clone for class Object performs a
* specific cloning operation. First, if the class of this object does
* not implement the interface Cloneable, then a
* CloneNotSupportedException is thrown. Note that all arrays
* are considered to implement the interface Cloneable.
* Otherwise, this method creates a new instance of the class of this
* object and initializes all its fields with exactly the contents of
* the corresponding fields of this object, as if by assignment; the
* contents of the fields are not themselves cloned. Thus, this method
* performs a "shallow copy" of this object, not a "deep copy" operation.
*
* The class Object does not itself implement the interface
* Cloneable, so calling the clone method on an object
* whose class is Object will result in throwing an
* exception at run time.
*
* @return a clone of this instance.
* @exception CloneNotSupportedException if the object's class does not
* support the Cloneable interface. Subclasses
* that override the clone method can also
* throw this exception to indicate that an instance cannot
* be cloned.
* @see java.lang.Cloneable
*/
protected native Object clone() throws CloneNotSupportedException;
}
呵呵,又是一大串的鸡肠,别以为我是来凑字数的,这些都是Sun公司Java开发人员写的技术文章,多看看少说话吧。
没错,又是个native方法,果然是个高深的东西,不过我们还是要占一下他的便宜。而且他这个方法是protected的,分明就是叫我们去占便宜的。
再继续看看下面测试代码。
import java.util.Date;
import org.junit.Test;
public class TestCase {
@Test
public void testUserClone() throws CloneNotSupportedException {
User u1 = new User("Kent", "123456", new Date());
User u2 = u1;
User u3 = (User) u1.clone();
System.out.println(u1 == u2); // true
System.out.println(u1.equals(u2)); // true
System.out.println(u1 == u3); // false
System.out.println(u1.equals(u3)); // true
}
}
这个clone()方法果然牛,一下子就把我们的对象克隆了一份,执行结果也符合我们的预期,u1和u3的地址不同但是内容相同。
Part IV
通过上述的例子,我们可以看出,要让一个对象进行克隆,其实就是两个步骤:
1. 让该类实现java.lang.Cloneable接口;
2. 重写(override)Object类的clone()方法。
但是,事实上真的是如此简单吗?再看下面的代码。
public class Administrator implements Cloneable {
private User user;
private Boolean editable;
public Administrator(User user, Boolean editable) {
this.user = user;
this.editable = editable;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public int hashCode() {
// 老规矩
}
@Override
public boolean equals(Object obj) {
// 老规矩
}
// 老规矩
}
上面定义了一个Administrator类,这个类持有一个User类的对象。接下来我们看看对Administrator对象进行克隆会有什么效果。
import java.util.Date;
import org.junit.Test;
public class TestCase {
@Test
public void testAdministratorClone() throws CloneNotSupportedException {
Administrator a1 = new Administrator(new User("Kent", "123456", new Date()), true);
Administrator a2 = a1;
Administrator a3 = (Administrator) a1.clone();
System.out.println(a1 == a2); // true
System.out.println(a1.equals(a2)); // true
System.out.println(a1 == a3); // false
System.out.println(a1.equals(a3)); // true
System.out.println(a1.getUser() == a3.getUser()); //true ! It's not our expected!!!!!
System.out.println(a1.getUser().equals(a3.getUser())); //true
}
}
呵呵呵!出问题了吧。Java哪是那么容易就能驾驭的说!
这里我们就可以引入两个专业的术语:浅克隆(shallow clone)和深克隆(deep clone)。
所谓的浅克隆,顾名思义就是很表面的很表层的克隆,如果我们要克隆Administrator对象,只克隆他自身以及他包含的所有对象的引用地址。
而深克隆,就是非浅克隆。克隆除自身以外所有的对象,包括自身所包含的所有对象实例。至于深克隆的层次,由具体的需求决定,也有“N层克隆”一说。
但是,所有的基本(primitive)类型数据,无论是浅克隆还是深克隆,都会进行原值克隆。毕竟他们都不是对象,不是存储在堆中。注意:基本数据类型并不包括他们对应的包装类。
如果我们想让对象进行深度克隆,我们可以这样修改Administrator类。
@Override
protected Object clone() throws CloneNotSupportedException {
Administrator admin = (Administrator) super.clone();
admin.user = (User) admin.user.clone();
return admin;
}
由于Boolean会对值进行缓存处理,所以我们没必要对Boolean的对象进行克隆。并且Boolean类也没有实现java.lang.Cloneable接口。
Part V
1. 让该类实现java.lang.Cloneable接口;
2. 确认持有的对象是否实现java.lang.Cloneable接口并提供clone()方法;
3. 重写(override)Object类的clone()方法,并且在方法内部调用持有对象的clone()方法;
4. ……
5. 多麻烦啊,调来调去的,如果有N多个持有的对象,那就要写N多的方法,突然改变了类的结构,还要重新修改clone()方法。
难道就没有更好的办法吗?
Part VI
接下来要重点介绍一下使用java.lang.Serializable来实现对象的深度克隆。
首先,我们编写一个工具类并提供cloneTo()方法。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public abstract class BeanUtil {
@SuppressWarnings("unchecked")
public static T cloneTo(T src) throws RuntimeException {
ByteArrayOutputStream memoryBuffer = new ByteArrayOutputStream();
ObjectOutputStream out = null;
ObjectInputStream in = null;
T dist = null;
try {
out = new ObjectOutputStream(memoryBuffer);
out.writeObject(src);
out.flush();
in = new ObjectInputStream(new ByteArrayInputStream(memoryBuffer.toByteArray()));
dist = (T) in.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (out != null)
try {
out.close();
out = null;
} catch (IOException e) {
throw new RuntimeException(e);
}
if (in != null)
try {
in.close();
in = null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return dist;
}
}
看不懂,没关系,直接拿去用就可以了。嘻嘻。
接下来我们测试一下是否能通过这个工具来实现深度克隆。
又是这个可爱的TestCase,可怜的每次都要动他……
import java.util.Date;
import org.junit.Test;
public class TestCase {
@Test
public void testCloneTo() {
Administrator src = new Administrator(new User("Kent", "123456", new Date()), true);
Administrator dist = BeanUtil.cloneTo(src);
System.out.println(src == dist); // false
System.out.println(src.equals(dist)); // true
System.out.println(src.getUser() == dist.getUser()); //false ! Well done!
System.out.println(src.getUser().equals(dist.getUser())); //true
}
}
好了,无论你的对象有多么的复杂,只要这些对象都能够实现java.lang.Serializable接口,就可以进行克隆,而且这种克隆的机制是JVM完成的,不需要修改实体类的代码,方便多了。
为什么这么简单就可以实现对象的克隆呢?java.lang.Serializable接口又是干嘛用的呢?如果想知道这些问题的答案,请汇款到0123 4567 8901 2345678。
……
开玩笑咔……这些问题的答案,我都留在了java.lang.Serializable的专题里面,请各位翻一下我的日志吧。PS.有可能这几天之内会出来,看哪天无聊写写吧……先睡够再说……
(转)Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨的更多相关文章
- Java 对象序列化和反序列化 (实现 Serializable 接口)
序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存放 ...
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
Java对象克隆(Clone)及Cloneable接口.Serializable接口的深入探讨 Part I 没啥好说的,直接开始Part II吧. Part II 谈到了对象的克隆,就不得不说为什么 ...
- 为什么集合类没有实现Cloneable和Serializable接口
为什么集合类没有实现Cloneable和Serializable接口? 答:克隆(cloning)或者序列化(serialization)的语义和含义是跟具体的实现相关的.因此应该由集合类的具体实现类 ...
- 为什么集合类没有实现Cloneable和Serializable接口?
为什么集合类没有实现Cloneable和Serializable接口? 克隆(cloning)或者是序列化(serialization)的语义和含义是跟具体的实现相关的.因此,应该由集合类的具体实现来 ...
- java 浅拷贝和深拷贝 对象克隆clone
分一下几点讨论: 为什么要克隆? 如何实现克隆 浅克隆和深克隆 解决多层克隆问题 总结 一:为什么要克隆? 大家先思考一个问题,为什么需要克隆对象?直接new一个对象不行吗? 答案是:克隆的对象可能包 ...
- Java对象克隆详解
原文:http://www.cnblogs.com/Qian123/p/5710533.html 假如说你想复制一个简单变量.很简单: int apples = 5; int pears = appl ...
- JAVA对象克隆可能会出现的问题
首先,区分一下拷贝和克隆: 拷贝:当拷贝一个变量时,原始变量与拷贝变量引用的是同一个对象.当改变一个变量所引用的对象,则会对另一个变量造成影响. 克隆:当克隆一个对象时,是重新的创建了和该对象内容相同 ...
- java对象克隆复制
原文链接:https://blog.csdn.net/ztchun/article/details/79110096 自己先简单描述总结一下:当想要将一个对象中已有的值直接给另外一个对象的时候,其实并 ...
- Java序列化接口Serializable接口的作用总结
一.Java序列化接口Serializable的作用: 一个对象有对应的一些属性,把这个对象保存在硬盘上的过程叫做”持久化”. 对象的默认序列化机制写入的内容是:对象的类,类签名,以及非瞬态和非静态字 ...
随机推荐
- carryLess开发日记_2017-05-18
1.接上一篇的form表单的ajax问题,上一篇中的form表单的ajax提交不能上传文件,所以采用了formData的方式上传 1)前段代码如下: <form action="&qu ...
- chroot jail
注意,原标题是:Linux Virtualization using Chroot Jail,我实在不知道怎么翻译,所以,自作主张,选了chroot jail作为标题.原文地址 chroot jail ...
- poi解析excel
一.遇见的问题: 当单元格设置为日期类型时,cell.getCellStyle().getDataFormat()返回的值都为176. poi jar包3.14以上不支持用cell.getCellTy ...
- 微信第三方登录(原生)demo
在一家ecstore二开公司有一段时间了,公司希望往自己研发产品上面走,一直在培养新人. 最近要自己去微信登录,自己就在ectore的框架基础上,写的原生微信第三方登录以此来熟悉微信第三方登录,在 ...
- R与并行计算(转)
文章摘要 本文首先介绍了并行计算的基本概念,然后简要阐述了R和并行计算的关系.之后作者从R用户的使用角度讨论了隐式和显示两种并行计算模式,并给出了相应的案例.隐式并行计算模式不仅提供了简单清晰的使用方 ...
- grant all赋权后mysql.user表权限字段还是N,但能登录和新建表
grant all赋权后mysql.user表权限字段还是N,但能登录和新建表 grant all privileges on linuxeye.* to linuxeye@'localhost' i ...
- 每天4亿行SQLite订单大数据测试(源码)
SQLite单表4亿订单,大数据测试 SQLite作为嵌入式数据库的翘楚,广受欢迎!新生命团队自2010年以来,投入大量精力对SQLite进行学习研究,成功应用于各系统非致命数据场合. SQLite极 ...
- 学习Spark——那些让你精疲力尽的坑
这一个月我都干了些什么-- 工作上,还是一如既往的写bug并不亦乐乎的修bug.学习上,最近看了一些非专业书籍,时常在公众号(JackieZheng)上写点小感悟,我刚稍稍瞄了下,最近五篇居然都跟技术 ...
- 乐视开放平台技术架构-servlet和spring mvc篇
在乐视风口浪尖的时候,敢于站出来说我是乐视的而不怕被打脸的,也就是我了.就算我以后不在乐视了,提起来在乐视工作过,我也还是挺骄傲的.因为这是一个有理想,敢拼敢干的公司.想起复仇者联盟里Fury指挥官的 ...
- 在附件管理模块中增加对FTP 上传和预览的支持
在之前介绍的附件管理模块里面<Winform开发框架之通用附件管理模块>以及<Winform开发框架之附件管理应用>,介绍了附件的管理功能,通过对数据库记录的处理和文件的管理, ...