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

clone 深拷贝 浅拷贝

时间:2015-06-11 18:48:35      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

1. 定义:知道一个对象,但不知道类,想要得到该对象相同的一个副本,在修改该对象的属性时,副本属性不修改,clone的是对象的属性

2. 意义:当一个对象里很多属性,想要得到一个相同的对象,还有set很多属性很麻烦

3. 实现:实现Cloneable接口(标识接口,里面没有方法的定义)标识该类对象可以克隆

            重写Object中的clone()方法,该方法可以合法的(实现上面接口)对该类实例(对象)按字段进行复制。

            在重写的clone()方法中 return super.clone(); (调用父类(Object)的clone方法,其实子类什么都没做)用父类的clone放复制当前对象的属性 返回复制的对象

4. 深拷贝,浅拷贝:对象属性有 基本类型 int String(不是基本但是final)等,引用变量(其他类的对象),因为基本基本类型变量的拷贝是值拷贝,引用变量的拷贝是拷贝的地址,所以克隆出来的对象的引用变量是同一个变量,在一个对象修改时,另一个对象也被修改,所以有了深拷贝。实现深拷贝需要把引用变量的对象的类也实现Cloneable接口,从写clone()方法,return super.clone(); 

5. 深拷贝好像用在java框架hibernate 上

/*
浅拷贝 s修改teacher s1的teacher也修改了
*/
package weiguoyuan.chainunicom.cn;

class Teacher1 {
    private String name;
    public Teacher1(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return name;
    }
}

class Student1 implements Cloneable{
    private String name;
    private Teacher1 teacher;
    public Student1(String name,Teacher1 teacher) {
        this.name = name;
        this.teacher = teacher;
    }
    public Teacher1 getTeacher() {
        return teacher;
    }
    public String toString() {
        return name+" "+teacher.toString();
    }
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
        
    }
}

public class TestClone {

    public static void main(String[] args) throws CloneNotSupportedException {
        Teacher1 t = new Teacher1("jianghongweiSB");
        Student1 s = new Student1("weiguoyuanNB",t);
        Student1 s1 = (Student1)s.clone();
        System.out.println(s);
        System.out.println(s1);
        
        s.getTeacher().setName("jianghongwei");
        System.out.println(s);
        System.out.println(s1);        
    }
}

 

/**
 * 深拷贝实现
 */
package weiguoyuan.chainunicom.cn;

class Teacher1 implements Cloneable{
    private String name;
    public Teacher1(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return name;
    }
    public Object clone() throws CloneNotSupportedException{
        return super.clone();    
    }
}

class Student1 implements Cloneable{
    private String name;
    private Teacher1 teacher;
    public Student1(String name,Teacher1 teacher) {
        this.name = name;
        this.teacher = teacher;
    }
    public Teacher1 getTeacher() {
        return teacher;
    }
    public String toString() {
        return name+" "+teacher.toString();
    }
    public Object clone() throws CloneNotSupportedException{
        Student1 student = (Student1)super.clone();
        student.teacher = (Teacher1)this.teacher.clone();
        return student;    
    }
}

public class TestClone {

    public static void main(String[] args) throws CloneNotSupportedException {
        Teacher1 t = new Teacher1("jianghongweiSB");
        Student1 s = new Student1("weiguoyuanNB",t);
        Student1 s1 = (Student1)s.clone();
        System.out.println(s);
        System.out.println(s1);
        
        s.getTeacher().setName("jianghongwei");
        System.out.println(s);
        System.out.println(s1);        
    }
}

 

 

            

clone 深拷贝 浅拷贝

标签:

原文地址:http://www.cnblogs.com/weixiaole/p/4569548.html

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