深克隆和浅克隆有什么区别?
深克隆(Deep Clone)是将原型对象中的所有类型,无论是值类型还是引用类型,都复制一份给克隆对象,也就是说深克隆会把原型对象和原型对象所引用的对象,都复制一份给克隆对象。
浅克隆(Shadow Clone)是把原型对象中成员变量为值类型的属性都复制给克隆对象,把原型对象中成员变量为引用类型的引用地址也复制给克隆对象,也就是原型对象中如果有成员变量为引用对象,则此引用对象的地址是共享给原型对象和克隆对象的。简单来说就是浅克隆只会复制原型对象,但不会复制它所引用的对象。
所以深克隆和浅克隆的主要区别在于:深克隆会复制原型对象和它所引用所有对象,而浅克隆只会复制原型对象。
如何实现深克隆?
深克隆的实现方式有很多种,大体可以分为以下几类:
- 所有对象都实现克隆方法;
- 通过构造方法实现深克隆;
- 使用 JDK 自带的字节流实现深克隆;
- 所有对象都实现克隆方法
这种方式需要让所有的引用对象都实现克隆(Cloneable 接口),示例代码如下:
public class CloneExample {
public static void main(String[] args) throws CloneNotSupportedException {
// 创建被赋值对象
Address address = new Address(001, "北京");
People p1 = new People(1, "Java", address);
// 克隆 p1 对象
People p2 = p1.clone();
// 修改原型对象
p1.getAddress().setCity("上海");
// 输出 p1 和 p2 地址信息
System.out.println("p1:" + p1.getAddress().getCity() +
" p2:" + p2.getAddress().getCity());
}
static class People implements Cloneable {
private Integer id;
private String name;
private Address address;
@Override
protected People clone() throws CloneNotSupportedException {
People people = (People) super.clone();
people.setAddress(this.address.clone());
// 引用类型克隆赋值
return people;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public People(Integer id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
}
static class Address implements Cloneable {
private Integer id;
private String city;
@Override
protected Address clone() throws CloneNotSupportedException {
return (Address) super.clone();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Address(Integer id, String city) {
this.id = id;
this.city = city;
}
}
}
这样 People 类就实现深克隆了。
- 通过构造方法实现深克隆
《Effective Java》 中推荐使用构造器(Copy Constructor)来实现深克隆,如果构造器的参数为基本数据类型或字符串类型则直接赋值,如果是对象类型,则需要重新 new 一个对象,实现代码如下:
public class SecondExample {
public static void main(String[] args) throws CloneNotSupportedException {
// 创建对象
Address address = new Address(001, "北京");
People p1 = new People(1, "Java", address);
// 调用构造函数克隆对象
People p2 = new People(p1.getId(), p1.getName(),
new Address(p1.getAddress().getId(), p1.getAddress().getCity()));
// 修改原型对象
p1.getAddress().setCity("上海");
// 输出 p1 和 p2 地址信息
System.out.println("p1:" + p1.getAddress().getCity() +
" p2:" + p2.getAddress().getCity());
}
static class People {
private Integer id;
private String name;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public People(Integer id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
}
static class Address {
private Integer id;
private String city;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Address(Integer id, String city) {
this.id = id;
this.city = city;
}
}
}
- 使用 JDK 自带的字节流实现深克隆
通过 JDK 自带的字节流实现深克隆的方式,是先将要原型对象写入到内存中的字节流,然后再从这个字节流中读出刚刚存储的信息,来作为一个新的对象返回,那么这个新对象和原型对象就不存在任何地址上的共享,这样就实现了深克隆,代码如下:
import java.io.*;
public class ThirdExample {
public static void main(String[] args) throws CloneNotSupportedException {
// 创建对象
Address address = new Address(001, "北京");
People p1 = new People(1, "Java", address);
// 通过字节流实现克隆
People p2 = (People) StreamClone.clone(p1);
// 修改原型对象
p1.getAddress().setCity("上海");
// 输出 p1 和 p2 地址信息
System.out.println("p1:" + p1.getAddress().getCity() +
" p2:" + p2.getAddress().getCity());
}
static class StreamClone {
public static <T extends Serializable> T clone(People obj) {
T cloneObj = null;
try {
// 写入字节流
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bo);
oos.writeObject(obj);
oos.close();
// 分配内存,写入原始对象,生成新对象
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());//获取上面的输出字节流
ObjectInputStream oi = new ObjectInputStream(bi);
// 返回生成的新对象
cloneObj = (T) oi.readObject();
oi.close();
} catch (Exception e) {
e.printStackTrace();
}
return cloneObj;
}
}
static class People implements Serializable {
private Integer id;
private String name;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public People(Integer id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
}
static class Address implements Serializable {
private Integer id;
private String city;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Address(Integer id, String city) {
this.id = id;
this.city = city;
}
}
}
此方式需要注意的是,由于是通过字节流序列化实现的深克隆,因此每个对象必须能被序列化,必须实现 Serializable 接口,标识自己可以被序列化,否则会抛出异常 (java.io.NotSerializableException)。