概述:
Java 对象深拷贝是指将一个对象完全复制另一个对象,即所有属性和属性值都被复制,并且两个对象之间没有相互影响。在 Java 开发中经常会使用对象深拷贝,比如在进行对象传参、克隆等场合都需要进行对象深拷贝。本文将详细讲解 Java 对象深拷贝工具类的实现。
实现:
Java 中提供了两种方式实现深拷贝:Serializable 和 Cloneable,下面分别进行介绍。
- Serializable 方式实现深拷贝
Serializable 序列化方式实现深拷贝,需要实现 Serializable 接口,并在对象的 writeObject 和 readObject 方法中进行序列化和反序列化。示例如下:
import java.io.*;
public class DeepCopyUtil {
public static <T> T deepCopy(T t) throws Exception {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(t);
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
return (T) oi.readObject();
}
static class Person implements Serializable {
private String name;
private int age;
private Address address;
public Person(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
}
static class Address implements Serializable {
private String city;
private String street;
}
public static void main(String[] args) throws Exception {
Address address = new Address();
address.city = "Beijing";
address.street = "Chang'an Street";
Person originPerson = new Person("Tom", 20, address);
Person copyPerson = deepCopy(originPerson);
System.out.println(copyPerson == originPerson);
System.out.println(copyPerson.address == originPerson.address);
}
}
输出结果:
false
false
从输出结果可以看出,通过 Serializable 方式实现深拷贝,对象被完全复制,两个对象没有相互影响,即使是对象中的属性也被完全复制。
- Cloneable 方式实现深拷贝
Cloneable 克隆方式实现深拷贝,需要将对象实现 Cloneable 接口,并在对象的 clone 方法中进行克隆。示例如下:
public class DeepCopyUtil {
public static <T extends Cloneable> T deepCopy(T t) throws Exception {
Method method = t.getClass().getDeclaredMethod("clone");
@SuppressWarnings("unchecked")
T copy = (T) method.invoke(t);
return copy;
}
static class Person implements Cloneable {
private String name;
private int age;
private Address address;
public Person(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
public Person clone() throws CloneNotSupportedException {
Person person = (Person) super.clone();
person.address = address.clone();
return person;
}
}
static class Address implements Cloneable {
private String city;
private String street;
public Address(String city, String street) {
this.city = city;
this.street = street;
}
public Address clone() throws CloneNotSupportedException {
return (Address) super.clone();
}
}
public static void main(String[] args) throws Exception {
Address address = new Address("Beijing", "Chang'an Street");
Person originPerson = new Person("Tom", 20, address);
Person copyPerson = deepCopy(originPerson);
System.out.println(copyPerson == originPerson);
System.out.println(copyPerson.address == originPerson.address);
}
}
输出结果:
false
false
从输出结果可以看出,通过 Cloneable 方式实现的深拷贝,对象也被完全复制,两个对象没有相互影响。
总结:
Java 对象深拷贝工具类的实现有多种方式可供选择,本文给出了两种方式,即 Serializable 和 Cloneable 方式。其中 Serializable 方式更加通用,但效率较低,而 Cloneable 方式效率较高,但需要实现 clone 方法,这对于一些已有类可能是不可行的。因此,在实际开发中,开发者需根据实际情况选择最合适的方式进行实现。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java 对象深拷贝工具类的实现 - Python技术站