温馨提示×

Java的深拷贝与浅拷贝怎么使用

小亿
89
2023-08-17 23:39:11
栏目: 编程语言

在Java中,对象的拷贝分为浅拷贝和深拷贝两种方式。

浅拷贝是指创建一个新的对象,然后将原对象的非静态字段值复制到新对象中。新对象和原对象的引用类型字段将指向相同的对象。可以通过实现Cloneable接口并重写clone()方法来实现浅拷贝。

深拷贝是指创建一个新的对象,然后将原对象的非静态字段值复制到新对象中。新对象和原对象的引用类型字段也将被复制,即引用类型字段指向的对象也会被复制。可以通过实现Serializable接口,将对象序列化为字节流,然后再反序列化为新对象来实现深拷贝。

下面是两种拷贝方式的示例代码:

  1. 浅拷贝示例:
class Person implements Cloneable {
private String name;
private int age;
private Address address;
// 省略构造方法和getter/setter
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Address {
private String city;
private String street;
// 省略构造方法和getter/setter
}
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Beijing", "Dongcheng");
Person person1 = new Person("Alice", 25, address);
Person person2 = (Person) person1.clone();
System.out.println(person1 == person2); // false
System.out.println(person1.getAddress() == person2.getAddress()); // true
}
}
  1. 深拷贝示例:
import java.io.*;
class Person implements Serializable {
private String name;
private int age;
private Address address;
// 省略构造方法和getter/setter
public Person deepCopy() throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return (Person) ois.readObject();
}
}
class Address implements Serializable {
private String city;
private String street;
// 省略构造方法和getter/setter
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Address address = new Address("Beijing", "Dongcheng");
Person person1 = new Person("Alice", 25, address);
Person person2 = person1.deepCopy();
System.out.println(person1 == person2); // false
System.out.println(person1.getAddress() == person2.getAddress()); // false
}
}

需要注意的是,深拷贝要求被拷贝的对象及其引用类型字段都必须实现Serializable接口。如果对象的某个引用类型字段没有实现Serializable接口,那么拷贝将会失败。

0