码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-05-12 15:30:30      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:相同   obj   str   getc   ali   概念   mic   tag   width   

一、浅复制和深复制概念

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

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

 

二、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)

标签:相同   obj   str   getc   ali   概念   mic   tag   width   

原文地址:https://www.cnblogs.com/linlf03/p/10850391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!