MENU

1994世界杯_1954年世界杯 - hengshuifu.com

为什么要使用克隆?如何实现对象克隆?深拷贝和浅拷贝区别是什么

有时需要复制一个对象,并且希望保留原有的对象进行接下来的操作。这时就需要使用克隆。

如何实现对象克隆?

实现Cloneable接口并重写clone方法:

实现​​Cloneable​​接口后,可以调用​​Object.clone()​​方法来创建一个新的对象。

通过序列化和反序列化实现深克隆:

将对象转换为字节流(序列化),然后再将字节流转回对象(反序列化)。

使用BeanUtils、Apache Commons或Spring等工具类库提供的bean工具:

这些工具提供了方便的克隆功能,但通常只能实现浅克隆。

深拷贝和浅拷贝的区别是什么?

浅拷贝:仅复制基本类型变量,不复制引用类型的变量。

深拷贝:既复制基本类型变量,又复制引用类型的变量。

示例代码解析

浅拷贝示例

public class Test {

public static void main(String[] args) {

Student student = new Student();

student.setId(1);

student.setName("张三");

Address address = new Address();

address.setDescribe("北京市朝阳区");

student.setAddress(address);

try {

Student cloneStu = (Student) student.clone();

System.out.println("------------------------------浅拷贝----------------------------------");

System.out.println("student == cloneStu: " + (student == cloneStu));

System.out.println("student.getAddress() == cloneStu.getAddress(): " + (student.getAddress() == cloneStu.getAddress()));

System.out.println("student.getAddress().getDescribe() == cloneStu.getAddress().getDescribe(): " + (student.getAddress().getDescribe() == cloneStu.getAddress().getDescribe()));

// 修改属性前

System.out.println(student.toString());

System.out.println(cloneStu.toString());

// 修改属性

student.setName("李四");

address.setDescribe("上海市浦东新区");

// 修改属性后

System.out.println(student.toString());

System.out.println(cloneStu.toString());

} catch (Exception e) {

e.printStackTrace();

}

}

}

深拷贝示例

public class DeepCloneTest implements Cloneable {

private String name;

private int age;

@Override

protected Object clone() throws CloneNotSupportedException {

return super.clone();

}

public static void main(String[] args) {

DeepCloneTest test = new DeepCloneTest();

test.name = "张三";

test.age = 20;

try {

DeepCloneTest cloneTest = (DeepCloneTest) test.clone();

System.out.println("------------------------------深拷贝----------------------------------");

System.out.println("test == cloneTest: " + (test == cloneTest));

System.out.println("test.name == cloneTest.name: " + (test.name == cloneTest.name));

System.out.println("test.age == cloneTest.age: " + (test.age == cloneTest.age));

// 修改属性前

System.out.println(test.toString());

System.out.println(cloneTest.toString());

// 修改属性

test.name = "李四";

test.age = 25;

// 修改属性后

System.out.println(test.toString());

System.out.println(cloneTest.toString());

} catch (Exception e) {

e.printStackTrace();

}

}

}

Copyright © 2022 1994世界杯_1954年世界杯 - hengshuifu.com All Rights Reserved.