Java中对象的深克隆和浅克隆

来源:互联网 发布:c语言c14准则 编辑:程序博客网 时间:2024/05/17 07:40

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

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

2.Java的clone()方法
⑴clone方法将对象复制了一份并返回给调用者。一般而言,clone()方法满足:
①对任何的对象x,都有x.clone() !=x//克隆对象与原对象不是同一个对象
②对任何的对象x,都有x.clone().getClass()= =x.getClass()//克隆对象与原对象的类型一样
③如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。

⑵Java中对象的克隆
①为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。

3.浅克隆的例子

public class Professor {    String name;    int age;    Professor(String name, int age) {        this.name = name;        this.age = age;    }}
public class Student  implements Cloneable {    String name;      int age;       Professor p;       Student(String name,int age,Professor p)       {       this.name=name;       this.age=age;       this.p=p;       }      public Object clone()       {        Student o=null;       try       {         o=(Student)super.clone();        }       catch(CloneNotSupportedException e)        {         System.out.println(e.toString());        }       return o;       }  }

public class MyMainTest {

public static void main(String[] args) {      Professor p=new Professor("wangwu",50);       Student s1=new Student("zhangsan",18,p);       Student s2=(Student)s1.clone();       s2.p.name="lisi";       s2.p.age=30;        System.out.println("name="+s1.p.name+","+"age="+s1.p.age);  //name=lisi,age=30      System.out.println("name="+s2.p.name+","+"age="+s2.p.age);  //name=lisi,age=30      Student s3=s1;      System.out.println(s3==s1);   //true      System.out.println(s2==s1);   //false      } }

从运行结果可以看出,学生1和学生2中的Professor 对象还是指向同一个对象,这就是浅克隆。

4 、深克隆的例子

class Professor implements Cloneable {    String name;    int age;    Professor(String name, int age) {        this.name = name;        this.age = age;    }    public Object clone() {        Object o = null;        try {            o = super.clone();        } catch (CloneNotSupportedException e) {            System.out.println(e.toString());        }        return o;    }}
public class Student implements Cloneable {    String name;    int age;    Professor p;    Student(String name, int age, Professor p) {        this.name = name;        this.age = age;        this.p = p;    }    public Object clone() {        Student o = null;        try {            o = (Student) super.clone();        } catch (CloneNotSupportedException e) {            System.out.println(e.toString());        }        // 对引用的对象也进行复制        o.p = (Professor) p.clone();        return o;    }}
public class MyMainTest {    public static void main(String[] args) {        Professor p = new Professor("wangwu", 50);        Student s1 = new Student("zhangsan", 18, p);        Student s2 = (Student) s1.clone();        s2.p.name = "lisi";        s2.p.age = 30;        System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age); // name=wangwu,age=50        System.out.println("name=" + s2.p.name + "," + "age=" + s2.p.age); // name=lisi,age=30    }}

从运行结果可以看出,学生1和学生2中的Professor 对象不再指向同一个对象,这就是深克隆。

阅读全文
0 0
原创粉丝点击