浅谈深复制
这个是前几天写代码的时候突然想到更的,有深复制就有浅复制,先来说一下这俩的概念。
对于基本数据类型使用等号赋值,实际上是拷贝的它的值。对于对象而言.
浅复制赋值的只是这个对象内存中的引用,也就是他们实际上还是指向的同一个对象。
而深复制则是新建了新的一个对象,且与被复制的属性相同
clone()方法
Java中所有的类的最终父类都是Object,好多面试官喜欢问Object里的方法,其中之一就是clone方法。
clone()是protect方法,只能在同一包下java.lang包或者子类里使用,而且要求必须实现Cloneable 接口,要不就抛异常。
实际调用internalClone()方法,这个属于native方法,暂时先不研究这个了。只需要知道它可以clone()一个对象得到一个新的对象实例就行。
代码试验下:
@Data
public class Person implements Cloneable{
public String pname;
public int page;
public Address address;
public Person() {}
public Person(String pname,int page){
this.pname = pname;
this.page = page;
this.address = new Address();
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public void setAddress(String provices,String city ){
address.setAddress(provices, city);
}
public void display(String name){
System.out.println(name+":"+"pname=" + pname + ", page=" + page +","+ address);
}
}
class Address {
private String provices;
private String city;
public void setAddress(String provices,String city){
this.provices = provices;
this.city = city;
}
@Override
public String toString() {
return "Address [provices=" + provices + ", city=" + city + "]";
}
}
@Test
public void testShallowClone() throws Exception{
Person p1 = new Person("zhangsan",21);
p1.setAddress("湖北省", "武汉市");
Person p2 = (Person) p1.clone();
System.out.println("p1:"+p1);
System.out.println("p1.getPname:"+p1.getPname().hashCode());
System.out.println("p2:"+p2);
System.out.println("p2.getPname:"+p2.getPname().hashCode());
p1.display("p1");
p2.display("p2");
p2.setAddress("湖北省", "荆州市");
System.out.println("将复制之后的对象地址修改:");
p1.display("p1");
p2.display("p2");
}
运行结果:
p1:Person@1540e19d
p1.getPname:-1432604556
p2:Person@677327b6
p2.getPname:-1432604556
p1:pname=zhangsan, page=21,Address [provices=湖北省, city=武汉市]
p2:pname=zhangsan, page=21,Address [provices=湖北省, city=武汉市]
将复制之后的对象地址修改:
p1:pname=zhangsan, page=21,Address [provices=湖北省, city=荆州市]
p2:pname=zhangsan, page=21,Address [provices=湖北省, city=荆州市]
显然clone方法成功复制了一个对象,但这是一次浅复制。因为我们修改了对象中的address之后两个对象都会变。
对象如何实现深复制
还是要从这个clone方法入手,其实只需要让Address类也实现Cloneable接口,重写clone方法就可以了。
不过这样会很蠢,因为如果Address类里也有引用类型就得再加,再有就得在家。所以,最好的方法是用序列化的方式。先序列化对象再反序列化回来就得到新的对象了
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
// 反序列化
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
List如何实现深复制
我们经常对数据库进行列表查询嘛,假如我们要在清洗之前拷贝一份用来别的处理,如果直接用等号复制那肯定是不想的。
所以还是要用序列化的方法。依靠来回序列化就可以得到新的对象啦。
public static <T extends Serializable> List<T> listDeepCopy(List<T> source) {
if (source == null) {
return Collections.emptyList();
}
List<T> copy = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(source);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
copy = (List<T>) in.readObject();
} catch (Exception e) {
throw new HoraException("列表克隆失败", e);
}
return copy;
}