1. 什么是浅克隆和深克隆?
❝克隆是指生物体通过体细胞进行的无性繁殖,以及由无性繁殖形成的基因型完全相同的后代个体。中学生物课本上的克隆羊多莉就属于这类生物。出处:百度百科
❞
而在 Java 领域,克隆 Java 基础的一部分,它是指快速地构建出一个已有对象的副本。
「浅克隆(Shadow Clone)「是把原型对象中」成员变量为值类型的属性都复制给克隆对象,把原型对象中成员变量为引用类型的引用地址也复制给克隆对象」,也就是「原型对象中如果有成员变量为引用对象,则此引用对象的地址是共享给原型对象和克隆对象的」。
说句人话就是,浅克隆只会复制原型对象,但不会复制它所引用的对象,我了方便理解,我画张图:
「深克隆(Deep Clone)「是将原型对象中的所有类型,无论是值类型还是引用类型,都复制一份给克隆对象,也就是说」深克隆会把原型对象和原型对象所引用的对象,都复制一份给克隆对象」,画张图理解:
2. 如何实现克隆?
在 Java 中实现克隆,「首先要需要实现 Cloneable 接口、其次重写 Object 类的 clone () 方法」,代码如下:
** * Project Name:review_java <br/> * Package Name:com.nasus.clone <br/> * Date:2021/1/31 20:10 <br/> * * @author <a href="turodog@foxmail.com">chenzy</a><br/> */ public class CloneExample { static class User implements Cloneable { // 年龄 private Integer age; // 名称 private String name; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } public static void main(String[] args) throws CloneNotSupportedException { // 创建对象 User userOne = new User(); userOne.setAge(22); userOne.setName("clone"); // 克隆 userOne User userTwo = (User) userOne.clone(); // 打印 System.out.println("userTwo: " + userTwo.getName()); } }
运行结果:
userTwo: clone
3. 克隆有什么规则?
要了解克隆的规则,就必须要从读源码开始,贴出源码:
/** * 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 {@code x}, the expression: * <blockquote> * <pre> * x.clone() != x</pre></blockquote> * will be true, and that the expression: * <blockquote> * <pre> * x.clone().getClass() == x.getClass()</pre></blockquote> * will be {@code true}, but these are not absolute requirements. * While it is typically the case that: * <blockquote> * <pre> * x.clone().equals(x)</pre></blockquote> * will be {@code true}, this is not an absolute requirement. * <p> * By convention, the returned object should be obtained by calling * {@code super.clone}. If a class and all of its superclasses (except * {@code Object}) obey this convention, it will be the case that * {@code x.clone().getClass() == x.getClass()}. * <p> * 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 {@code 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 {@code super.clone} * need to be modified. * <p> * The method {@code clone} for class {@code Object} performs a * specific cloning operation. First, if the class of this object does * not implement the interface {@code Cloneable}, then a * {@code CloneNotSupportedException} is thrown. Note that all arrays * are considered to implement the interface {@code Cloneable} and that * the return type of the {@code clone} method of an array type {@code T[]} * is {@code T[]} where T is any reference or primitive type. * 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. * <p> * The class {@code Object} does not itself implement the interface * {@code Cloneable}, so calling the {@code clone} method on an object * whose class is {@code Object} will result in throwing an * exception at run time. * * @return a clone of this instance. * @throws CloneNotSupportedException if the object's class does not * support the {@code Cloneable} interface. Subclasses * that override the {@code 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;
因为是 native 方法,所以看代码没啥好看的。主要看注释,clone () 方法的规则主要有三条:
- 对于所有对象,x.clone () !=x 应当返回 true,因为克隆对象与原对象不是同一个对象。
- 对于所有对象,x.clone ().getClass () == x.getClass () 应当返回 true,因为克隆对象与原对象的类型是一样的。
- 对于所有对象,x.clone ().equals (x) 应当返回 true,因为使用 equals 比较时,它们的值都是相同的。
4. Arrays.copyOf () 是深克隆还是浅克隆?
对于数组类型的克隆,我们一般使用 Arrays.copy () 实现,代码如下:
// 原型对象 User[] userArrayOne = {new User(22, "Java")}; // 克隆对象 User[] userArrayTwo = Arrays.copyOf(userArrayOne, userArrayOne.length); // 修改克隆对象的第一个元素的值 userArrayTwo[0].setAge(25); // 打印 System.out.println("userOne age : " + userArrayOne[0].getAge()); System.out.println("userTwo age : " + userArrayTwo[0].getAge());
运行结果:
userOne age : 25 userTwo age : 25
注意这段代码的第七句,我改变了克隆对象的 age 属性值。紧接着打印输出二者的 age 属性值。发现两个属性值是一样的。为什么呢?
深浅克隆的区别是浅克隆只会复制原型对象,并不会复制它所引用的对象。而深克隆会把引用的对象也给复制了。而在这里,「数组是原型对象,它所引用的对象是里面的 User。」
「我改变了克隆对象其中的 User 的 age,紧接着发现原型对象中的 User 的 age 属性也改变了。这就说明了,Arrays.copy () 是浅克隆,两个对象数组中的 User 只是地址值,它两都指向同一个 User 引用对象」。
如果是深克隆,不管是修改原型对象还是引克隆对象中的 User 属性值,另一个应当是不变的。
PS:反之,修改原型对象中的 User 的属性值,克隆对象中对应引用对象的属性值也会改变。