标签:java
一.浅克隆和深克隆的概念:
1).浅克隆:又称为浅复制,被复制的对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换而言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
2).深克隆:又称为深复制,被复制的对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换而言之,深复制就是把要复制的对象所引用的对象都复制了一遍。
二.浅克隆实现:
<pre class="java" name="code"><strong><span style="font-size:18px;">public class Main { public static void main(String[] args) throws Exception { Student student1 = new Student(); student1.setName("张三"); student1.setSex("男"); student1.setNumber(10); Student student2 = (Student)student1.clone(); System.out.println(student2.getName() +" "+ student2.getNumber() +" "+ student2.getSex()); student1.setName("李四"); System.out.println(student1.getName()); System.out.println(student2.getName()); System.out.println(student1 == student2); System.out.println(student1.getClass() == student2.getClass()); } } class Student implements Cloneable{ int number; String name; String sex; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override protected Object clone() throws CloneNotSupportedException { Object obj = super.clone(); return obj; } }</span></strong>
打印:
张三 10 男
李四
张三
false
true
Object的clone方法就是浅克隆,clone()方法满足(1).对任何对象obj,都有obj.clone()!=obj,克隆的对象与原对象不是一个对象。
三.深克隆实现(这种方式较为麻烦,一般用序列化实现):
public class Main { public static void main(String[] args) throws Exception { Teacher teacher = new Teacher(); teacher.setName("李四"); teacher.setNumber(20); Student stu = new Student(); stu.setName("张三"); stu.setNumber(10); stu.setSex("男"); stu.setTeacher(teacher); Student stu2 = (Student)stu.clone(); System.out.println(stu2.getTeacher().getName()); teacher.setName("王五"); //如果是浅克隆则打印王五,因为teacher引用没变,变得时候teacher中的name属性 System.out.println(stu2.getTeacher().getName()); } } class Teacher implements Cloneable{ int number; String name; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } class Student implements Cloneable{ int number; String name; String sex; Teacher teacher; public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override protected Object clone() throws CloneNotSupportedException { Student student = (Student)super.clone(); student.setTeacher((Teacher)student.getTeacher().clone()); return student; } }
打印:
李四
李四
标签:java
原文地址:http://blog.csdn.net/u010708662/article/details/44871429