Java的浅拷贝和深拷贝

首先需要明白,浅拷贝和深拷贝都是针对一个已有对象的操作。那先来看看浅拷贝和深拷贝的概念。

在 Java 中,除了基本数据类型(元类型)之外,还存在 类的实例对象 这个引用数据类型。而一般使用 『 = 』号做赋值操作的时候。对于基本数据类型,实际上是拷贝的它的值,但是对于对象而言,其实赋值的只是这个对象的引用,将原对象的引用传递过去,他们实际上还是指向的同一个对象。

而浅拷贝和深拷贝就是在这个基础之上做的区分,如果在拷贝这个对象的时候,只对基本数据类型进行了拷贝,而对引用数据类型只是进行了引用的传递,而没有真实的创建一个新的对象,则认为是浅拷贝。反之,在对引用数据类型进行拷贝的时候,创建了一个新的对象,并且复制其内的成员变量,则认为是深拷贝。

接下来我们通过例子来分析

Java 中的 clone()方法
public class ParentBean implements Cloneable {
    private ChildBean child;
    
    public ParentBean(ChildBean child) {
        this.child = child;
    }
    
    public ChildBean getChild() {
        return child;
    }
    
    public void setChild(ChildBean child) {
        this.child = child;
    }
    
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public ParentBean clone() {
        try {
            return (ParentBean) super.clone();
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }
}

public class ChildBean {
    private String name;
    
    public ChildBean(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}

运行以下调试代码

ParentBean p1 = new ParentBean(new ChildBean("child"));
ParentBean p2;
p2 = p1.clone();
p1.getChild().setName("child change");
        
Log.i("msg", "p1 equals p2: " + p1.equals(p2));
Log.i("msg", "p1 child HashCode: " + p1.getChild().hashCode());
Log.i("msg", "p2 child HashCode: " + p2.getChild().hashCode());
Log.i("msg", "b2: " + p2.getChild().getName());

输出结果

I/msg: p1 equals p2: false
I/msg: p1 child HashCode: 142701218
I/msg: p2 child HashCode: 142701218
I/msg: b2: child change

p1 equals p2: false看似p2深拷贝一个新的p1对象,但从里面对象child的地址可以看到,还是仅仅做了传值的操作,当p1中的值做改变的时候,p2child也发生了变化,所以这还是一个浅拷贝。
那怎么才能实现深拷贝呢,其实要对每一个自定义的对象都要实现Cloneable接口,并重写方法clone(),比如上面中的例子,需要ChlidBean也同样实现Cloneable,并且在ParentBeanclone()方法中对child重新赋值。如下:

public class ChildBean implements Cloneable {
    private String name;
    
    public ChildBean(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public ChildBean clone() {
        try {
            return (ChildBean) super.clone();
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }
}

ParentBean:

@TargetApi(Build.VERSION_CODES.N)
@Override
public ParentBean clone() {
    try {
        ParentBean parent = (ParentBean) super.clone();
        parent.setChild(this.getChild().clone());
        return parent;
    } catch (CloneNotSupportedException e) {
        // this shouldn't happen, since we are Cloneable
        throw new InternalError(e);
    }
}

然后重写运行下调试代码,输出结果如下:

I/msg: p1 equals p2: false
I/msg: p1 child HashCode: 142701218
I/msg: p2 child HashCode: 190274355
I/msg: b2: child

p1 childp2 childHashCode值不一样,所指向的地址也发生了变化,当p1中的值做改变的时候,p2child并没有发生变化。故这样就完成了一次深拷贝。

实则浅拷贝和深拷贝只是相对的,如果一个对象内部只有基本数据类型,那用 clone()方法获取到的就是这个对象的深拷贝,而如果其内部还有引用数据类型,那用clone()方法就是一次浅拷贝的操作。

扩展阅读

如果我们查看java源码, 可以发现, 我们调用的clone()方法是Object对象的. 而不是Cloneable接口的. 那么我们为什么要实现Cloneable接口呢? 不实现Cloneable接口可否调用Objectclone()方法呢?

我们先来看下Cloneable接口的源码:

public interface Cloneable {
}

发现其中并没有任何方法. 幸运的是Java源码的java doc注释足够清晰:

/**
 * A class implements the <code>Cloneable</code> 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.
 * <p>
 * Invoking Object's clone method on an instance that does not implement the
 * <code>Cloneable</code> interface results in the exception
 * <code>CloneNotSupportedException</code> being thrown.
 * <p>
 * By convention, classes that implement this interface should override
 * <tt>Object.clone</tt> (which is protected) with a public method.
 * See {@link java.lang.Object#clone()} for details on overriding this
 * method.
 * <p>
 * Note that this interface does <i>not</i> contain the <tt>clone</tt> 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.
 */

大体我们可以理解几点:

Cloneable可以看着是一个标识, 实现了改接口的类才能合法地调用其从Object类中继承而来的clone()方法.
如果没有实现Cloneable接口而调用clone()方法, 会触发CloneNotSupportedException异常.
实现Cloneable接口的类应当重写Objectclone()方法.

参考

https://my.oschina.net/jackieyeah/blog/206391
https://segmentfault.com/a/1190000010648514
https://www.jianshu.com/p/ca5abfca2c6d

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。