关于clone对象的拷贝

更新时间:2019-05-17 15:30:50点击次数:436次
clone为Object类的一个本地方法

protected native Object clone() throws CloneNotSupportedException;

要想对象具有拷贝功能则需要该类实现Cloneable接口,表示该类允许拷贝,并且在类中自定义clone调用Object类提供的继承权限clone方法。
关于对象的两种拷贝方式
1.浅拷贝

        class Teacher implements Cloneable {
            private String name;
            private Integer age;
            public Teacher(String name, Integer age) {
                this.name = name;
                this.age = age;
            }
            @Override
            protected Object clone() throws CloneNotSupportedException {
                return super.clone();
            }
        }
        class Student implements Cloneable {
        private String name;
        private Integer age;
        private Teacher teacher;
        public Student(String name, Integer age, Teacher teacher) {
            this.name = name;
            this.age = age;
            this.teacher = teacher;
        }
         public Teacher getTeacher() {
            return teacher;
        }
        public void setTeacher(Teacher teacher) {
            this.teacher = teacher;
        }
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    public class TestClone {
        public static void main(String[] args) throws Exception {
            Teacher teacher = new Teacher("lxy", 17);
            Student student = new Student02("abc", 12, teacher);
            Student clone = (Student)student.clone();
            System.out.println(student == clone);//false
             System.out.println(student.getTeacher()==clone.getTeacher());//true
        }
    }

由上面结果可以看出,浅拷贝出来的对象保留着原对象的中其他类的引用,在拷贝出来的对象中没用创建其他类新的对象。
2.深拷贝
深拷贝:对拷贝出来的对象对于其所有引用创建产生新的对象。
实现方式:
1,包含的其他类实现cloneable接口,并且其他类的拷贝是来自自身类的clone方法
2,使用序列化,本类和本类包含的其他类实现Serializable。
1.代码实现

class Teacher implements Cloneable {
    private String name;
    private Integer age;
    public Teacher(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable {
    private String name;
    private Integer age;
    private Teacher teacher;

    public Student(String name, Integer age, Teacher teacher) {
        this.name = name;
        this.age = age;
        this.teacher = teacher;
    }
    @Override
    protected Student clone() throws CloneNotSupportedException {
        Student temp = (Student) super.clone();
        temp.teacher = (Teacher) this.teacher.clone();
        return temp;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
}
public class TestClone{
    public static void main(String[] args) throws Exception {
        Teacher teacher = new Teacher("lxy", 17);
        Student student = new Student("abc", 12, teacher);
        Student clone =student.clone();
        System.out.println(student == clone);//false
        System.out.println(student.getTeacher()==clone.getTeacher());//false
    }
}

2.代码实现

class Teacher implements Serializable {
    private String name;
    private Integer age;
    public Teacher(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
class Student implements Serializable {
    private String name;
    private Integer age;
    private Teacher teacher;
    public Student(String name, Integer age, Teacher teacher) {
        this.name = name;
        this.age = age;
        this.teacher = teacher;
    }
    public Student cloneObject() throws IOException, ClassNotFoundException {
        ByteOutputStream bos = new ByteOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.getBytes());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Student) ois.readObject();
    }
 public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
}
  public class TestClone {
        public static void main(String[] args) throws Exception {
            Teacher teacher = new Teacher("lxy", 17);
            Student student = new Student02("abc", 12, teacher);
            Student clone =student.clone();
            System.out.println(student == clone);//false
             System.out.println(student.getTeacher()==clone.getTeacher());//false
        }
    }

由结果可知拷贝出来的对象中对于其他对象的引用为新值。对任意对象的修改都不会对其他对象产生影响。

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息