java深克隆与浅克隆的区别原理及其实现

一、为什么要克隆?

答案是:克隆的对象可能包含一些已经修改过的属性,保留着你想克隆对象的值,而new出来的对象的属性全是一个新的对象,对应的属性没有值,所以我们还要重新给这个对象赋值。即当需要一个新的对象来保存当前对象的“状态”就靠clone方法了。那么我把这个对象的临时属性一个一个的赋值给我新new的对象不也行嘛?可以是可以,但是一来麻烦不说,二来,大家通过上面的源码都发现了clone是一个native方法,就是快啊,在底层实现的。
 

二、如何实现克隆

分三步:

  1. 对象的类实现Cloneable接口;
  2. 覆盖Object类的clone()方法 (覆盖clone()方法,访问修饰符设为public,默认是protected);
  3. 在clone()方法中调用super.clone();

三、两种不同的克隆方法,浅克隆(ShallowClone)和深克隆(DeepClone)。

浅克隆是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。

深克隆不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。举例来说更加清楚:

浅克隆

package cn.zhm.day4;
 
public class Student implements Cloneable {
	private int age;
	private String name;
 
	public Student(int age, String name) {
		this.age = age;
		this.name = 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 String toString() {
		return "Student [age=" + age + ", name=" + name + "]";
	}
 
	@Override
	public Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
 
	/**
	 * @param args
	 * @throws CloneNotSupportedException
	 */
	public static void main(String[] args) throws CloneNotSupportedException {
		Student student1 = new Student(20, "张三");
		Student student2 = (Student) student1.clone();
		student2.setAge(22);// 注意修改student2的age值 但是没有影响 student1的值
		System.out.println("student1:" + student1.getName() + "-->"+ student1.getAge());
		System.out.println("student2:" + student2.getName() + "-->"+ student2.getAge());
 
	}
}
运行结果:
student1:张三-->20
student2:张三-->22 

java深克隆与浅克隆的区别原理及其实现_第1张图片

引入深克隆,浅入克隆导致的问题

package cn.zhm.day4;
 
class Teacher implements Cloneable {
	private String name;
	private Student student;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Override
	public String toString() {
		return "Teacher [name=" + name + ", student=" + student + "]";
	}
	
	@Override
	public Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	public static void main(String[] args) throws CloneNotSupportedException {
		Student s1 = new Student();
		s1.setAge(20);
		s1.setName("张三");
		Teacher teacher1 = new Teacher();
		teacher1.setName("小赵老师");
		teacher1.setStudent(s1);
		//为什么会出现以下结果, Teacher中的clone方法
		Teacher teacher2 = (Teacher)teacher1.clone();
		Student s2 = teacher2.getStudent();
		s2.setName("李四");
		s2.setAge(30);
		System.out.println("teacher1:"+teacher1);
		System.out.println("teacher2:"+teacher2);
		
	}
	
}

运行结果:

teacher1:Teacher [name=小赵老师, student=Student [age=30, name=李四]]
teacher2:Teacher [name=小赵老师, student=Student [age=30, name=李四]

 

深克隆

要克隆的类和类中所有非基本数据类型的属性对应的类

必需在clone()方法注意点

package cn.zhm.day4;
 
class Teacher implements Cloneable {
    private String name;
    private Student student;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    @Override
    public String toString() {
        return "Teacher [name=" + name + ", student=" + student + "]";
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        //注意以下代码
        Teacher teacher = (Teacher)super.clone();
        teacher.setStudent((Student)teacher.getStudent().clone());
        return teacher;
    }
    public static void main(String[] args) throws CloneNotSupportedException {
        Student s1 = new Student();
        s1.setAge(20);
        s1.setName("张三");
        Teacher teacher1 = new Teacher();
        teacher1.setName("小赵老师");
        teacher1.setStudent(s1);
        Teacher teacher2 = (Teacher)teacher1.clone();
        teacher2.setName("小明老师");
        Student s2 = teacher2.getStudent();
        s2.setName("李四");
        s2.setAge(30);
        System.out.println("teacher1:"+teacher1);
        System.out.println("teacher2:"+teacher2);
        
    }
    
}

运行结果:

teacher1:Teacher [name=小赵老师, student=Student [age=20, name=张三]]

teacher2:Teacher [name=小明老师, student=Student [age=30, name=李四]]

好好理解上面代码一定可以掌握 浅克隆(ShallowClone)和深克隆(DeepClone)
 

你可能感兴趣的:(java基础,java)