一、浅拷贝与深拷贝
拷贝对象在Java程序中是一个常见的操作,通常有两种拷贝方式:浅拷贝和深拷贝。
浅拷贝是指只复制了对象的引用,如果原对象的引用类型属性被修改了,那么拷贝后的对象的引用类型属性也会被修改。此时Java中的clone()方法默认实现就是浅拷贝,只需要实现Cloneable接口,重写clone()方法即可实现浅拷贝。
public class Person implements Cloneable {
private String name;
private int age;
private List hobbies;
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
Person p1 = new Person();
p1.setName("Alice");
p1.setAge(20);
List list = new ArrayList();
list.add("reading");
list.add("writing");
p1.setHobbies(list);
Person p2 = (Person)p1.clone();
System.out.println(p2.getName()); // Alice
System.out.println(p2.getHobbies()); // [reading, writing]
list.add("swimming");
System.out.println(p1.getHobbies()); // [reading, writing, swimming]
System.out.println(p2.getHobbies()); // [reading, writing, swimming]
深拷贝则是将原对象的引用类型属性也复制一份出来,独立于原对象以及拷贝出来的对象之外。这样在修改原对象引用类型属性时,拷贝出来的对象不会受到影响。常见的实现方式有序列化与反序列化、Apache Commons BeanUtils、Spring BeanUtils等。
//使用序列化与反序列化实现深拷贝
public static T deepClone(T obj) throws IOException, ClassNotFoundException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(obj);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(in);
return (T)ois.readObject();
}
Person p1 = new Person();
p1.setName("Alice");
p1.setAge(20);
List list = new ArrayList();
list.add("reading");
list.add("writing");
p1.setHobbies(list);
Person p2 = deepClone(p1);
System.out.println(p2.getName()); // Alice
System.out.println(p2.getHobbies()); // [reading, writing]
list.add("swimming");
System.out.println(p1.getHobbies()); // [reading, writing, swimming]
System.out.println(p2.getHobbies()); // [reading, writing]
二、实现Cloneable接口
实现Cloneable接口是实现对象拷贝功能的一种方式,但是需要注意以下几点:
1、实现Cloneable接口并重写clone()方法时,clone()方法的访问控制符应该为public,并且方法内部需要进行异常处理。
2、被拷贝的对象及其引用类型属性都需要实现Cloneable接口或者是基本数据类型。
3、实现Cloneable接口的对象属性不能使用final修饰符。
三、序列化与反序列化实现深拷贝
序列化与反序列化也是一种常见的实现深拷贝的方式,具体实现方法可以使用Java的序列化机制,序列化时将对象写入到字节流中,反序列化时从字节流中读取对象。实现方式如下:
public static T deepClone(T obj) throws IOException, ClassNotFoundException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(obj);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(in);
return (T)ois.readObject();
}
Person p1 = new Person();
p1.setName("Alice");
p1.setAge(20);
List list = new ArrayList();
list.add("reading");
list.add("writing");
p1.setHobbies(list);
Person p2 = deepClone(p1);
System.out.println(p2.getName()); // Alice
System.out.println(p2.getHobbies()); // [reading, writing]
list.add("swimming");
System.out.println(p1.getHobbies()); // [reading, writing, swimming]
System.out.println(p2.getHobbies()); // [reading, writing]
四、Apache Commons BeanUtils实现深拷贝
Apache Commons BeanUtils是一个Java类库,提供各种操作JavaBean的工具,其中就包括实现深拷贝的方法。但是使用时需要注意以下问题:
1、实现深拷贝的对象属性需要提供get和set方法,并且对应的属性也需要提供get和set方法。
2、不支持普通的List、Set等集合对象的深拷贝,需要使用Apache Commons Collections提供的工具类。
public static T deepClone(T obj) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
T newObj = (T)obj.getClass().newInstance();
BeanUtils.copyProperties(newObj, obj);
return newObj;
}
Person p1 = new Person();
p1.setName("Alice");
p1.setAge(20);
List list = new ArrayList();
list.add("reading");
list.add("writing");
p1.setHobbies(list);
Person p2 = deepClone(p1);
System.out.println(p2.getName()); // Alice
System.out.println(p2.getHobbies()); // [reading, writing]
list.add("swimming");
System.out.println(p1.getHobbies()); // [reading, writing, swimming]
System.out.println(p2.getHobbies()); // [reading, writing]
五、Spring BeanUtils实现深拷贝
Spring BeanUtils也提供了一种实现深拷贝的方法,与Apache Commons BeanUtils类似,但是Spring BeanUtils需要使用反射机制,使用时需要注意以下问题:
1、实现深拷贝的对象属性需要提供get和set方法,并且对应的属性也需要提供get和set方法。
2、如果对象中有Date或者Calendar类型的属性,需要设置特殊的转换器。
public static T deepClone(T obj) {
T newObj = (T)ReflectionUtils.newInstance(obj.getClass());
BeanUtils.copyProperties(obj, newObj);
return newObj;
}
Person p1 = new Person();
p1.setName("Alice");
p1.setAge(20);
List list = new ArrayList();
list.add("reading");
list.add("writing");
p1.setHobbies(list);
Person p2 = deepClone(p1);
System.out.println(p2.getName()); // Alice
System.out.println(p2.getHobbies()); // [reading, writing]
list.add("swimming");
System.out.println(p1.getHobbies()); // [reading, writing, swimming]
System.out.println(p2.getHobbies()); // [reading, writing]
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/289107.html
微信扫一扫
支付宝扫一扫