深拷贝(deep clone)与浅拷贝(shallow clone)

Posted linlf03

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深拷贝(deep clone)与浅拷贝(shallow clone)相关的知识,希望对你有一定的参考价值。

一、浅复制和深复制概念

浅复制(浅克隆): 被复制对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不是复制它所引用的对象。

深复制(深克隆): 被复制对象的所有变量都含有与原来对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象, 而不再试原有的那些被引用的对象。换言之,深复制把复制的对象所应用的对象都复制了一遍。  

 

二、Java的Clone()方法 【定义在Object类中】

clone方法将对象复制一份并返回给调用者。

一般而言,clone()  方法满足:

1、对任何对象x,都有x.clone() != x

克隆对象与原对象不是一个对象

2、对任何的对象x,都有

x.clone().getClass() == x.getClass()

克隆对象与原对象的类型一样

3、如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立

 

 

 

二、浅复制Demo

 

public class CloneTest1 {

    public static void main(String[] args) throws CloneNotSupportedException {
        Student student = new Student();
        student.setAge(20);
        student.setName("Larry");
        
        Student student2 = (Student)student.clone();
        System.out.println(student2.getAge());
        System.out.println(student2.getName());
            
        System.out.println("-----------------------");

        student2.setName("Nick");
        System.out.println(student.getName());
        System.out.println(student2.getName());
        
    }
}

class Student implements Cloneable{
    private int age;
    private String name;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    
    @Override
    public Object clone() throws CloneNotSupportedException {
        Object object = super.clone();
        return object;
    }
}

打印结果

20
zhangsan
-----------------------
zhangsan
Lisi

 student2.setName("Nick"); 执行后,student2的name赋予新的值,指向新的空间。如下图

技术图片

 

三、深复制
以下代码还是浅复制
public class CloneTest2 {

    public static void main(String[] args) throws CloneNotSupportedException {
        
        Teacher teacher = new Teacher();
        teacher.setAge(40);
        teacher.setName("Teacher zhang");
        
        Student2 s1 = new Student2();
        s1.setAge(20);
        s1.setName("zhangsan");
        s1.setTeacher(teacher);
        
        Student2 s2 = (Student2)s1.clone();
        System.out.println(s2.getAge());
        System.out.println(s2.getName());
        
        teacher.setName("Teacher Li");
        
        System.out.println(s2.getTeacher().getAge());
        System.out.println(s2.getTeacher().getName());
            

        
    }
}

class Teacher {
    private int age;
    private String name;
    

    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

class Student2 implements Cloneable{
    private int age;
    private String name;
    
    private Teacher teacher;
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    
    
    
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        Object object = super.clone();
        return object;
    }
}

 

修改成深复制

Teacher也实现Clone方法

技术图片

 

然后改写Student的Clone方法

技术图片

clone Student的时候,修改Teacher。

 

 

修改后的完整代码:

public class CloneTest2 {

	public static void main(String[] args) throws CloneNotSupportedException {
		
		Teacher teacher = new Teacher();
		teacher.setAge(40);
		teacher.setName("Teacher zhang");
		
		Student2 s1 = new Student2();
		s1.setAge(20);
		s1.setName("zhangsan");
		s1.setTeacher(teacher);
		
		Student2 s2 = (Student2)s1.clone();
		System.out.println(s2.getAge());
		System.out.println(s2.getName());
		
		teacher.setName("Teacher Li");
		
		System.out.println(s2.getTeacher().getAge());
		System.out.println(s2.getTeacher().getName());
			

		
	}
}

class Teacher implements Cloneable{
	private int age;
	private String name;
	

	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	

	@Override
	public Object clone() throws CloneNotSupportedException {
		
		return super.clone();
	}
	
}

class Student2 implements Cloneable{
	private int age;
	private String name;
	
	private Teacher teacher;
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	@Override
	public Object clone() throws CloneNotSupportedException {
		Student2 student2 = (Student2)super.clone();
		student2.setTeacher((Teacher)student2.getTeacher().clone());
		return student2;
	}
}

  

 

五、利用序列化来做深复制

把对象写到流里的过程是序列化(Serilization)过程,而把对象从流中读取出来的过程则叫饭序列化(Deserialization)过程。应当指出的是,写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面

 

使用Clone方法实现深复制存在的缺点: 存在多个引用的成员变量,将做多次处理。

技术图片

如上图做了一次处理,如果有多个应用,将做多次处理。

 

以上是关于深拷贝(deep clone)与浅拷贝(shallow clone)的主要内容,如果未能解决你的问题,请参考以下文章

java 深拷贝与浅拷贝机制详解

(转载)深拷贝与浅拷贝

java中的深拷贝与浅拷贝

JavaSE知识集锦深拷贝与浅拷贝

浅谈浅克隆(shallow clone)和 深克隆(deep clone)

python深拷贝与浅拷贝