如何在java中进行浅层克隆和深度克隆?

时间:2017-04-21 04:50:00

标签: java cloning

我在包pack1中有三个类。这三个类是classA classBclassC

classA

public class Address {
    public String town = null;
    public String street = null;
    public int postCode = 0;
    public int houseNumber = 0;
}

classB

public class Course {
    public String number;
    public String name;

    public Course(){
        number = null;
        name = null;
    }

classC

public class Student {
    public Date dob;
    public Course course = new Course();
    public Address address = new Address();


    public Student(){
        dob = null;
        course.name = null;
        course.number = null;
        address.town = null;
        address.street = null;
        address.postCode = 0;
        address.houseNumber = 0;
        course.name = null;
        course.number = null;

    }

我想知道我如何深入克隆地址和dob以及浅层克隆课程?我不知道如何进行克隆组合

3 个答案:

答案 0 :(得分:2)

ShallowCopy: 对象的浅表副本将具有原始对象的所有字段的精确副本。如果原始对象具有对其他对象的任何引用作为字段,则只将这些对象的引用复制到克隆对象中,不会创建这些对象的副本。

深层复制: 对象的深层副本将具有原始对象的所有字段的精确副本,就像浅层副本一样。但另外,如果原始对象有任何对其他对象的引用作为字段,那么也可以通过调用它们上的clone()方法来创建这些对象的副本

由于课程实体没有任何对象参考,因此使用defalut克隆方法进行克隆。 学生实体作为日期,课程,地址的参考我们需要覆盖克隆方法。 以下是示例代码: -

public static class Address implements Cloneable{
    public String town = null;
    public String street = null;
    public int postCode = 0;
    public int houseNumber = 0;

    public Address(String town , String street ,int postCode , int houseNumber){
        this.town = town;
        this.street = street;
        this.postCode = postCode;
        this.houseNumber = houseNumber;
    }
    public Address(){
    }

    //Default version of clone() method. It creates shallow copy of an object.

    protected Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }
}

public static class Course implements Cloneable{
    public String number;
    public String name;

    public Course(){
        number = null;
        name = null;
    }

    public Course(String number , String name){
        this.number = number;
        this.name = name;
    }

  //Default version of clone() method. It creates shallow copy of an object.

    protected Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }

} 

 public static class Student implements Cloneable{
        public Date dob;
        public Course course = new Course();
        public Address address = new Address();



        public Student(){
            dob = null;
            course.name = null;
            course.number = null;
            address.town = null;
            address.street = null;
            address.postCode = 0;
            address.houseNumber = 0;

        }

        public Student(Date dob , Course course , Address address){
            this.dob = dob;
            this.course = course;
            this.address = address;
        }

        protected Object clone() throws CloneNotSupportedException
        {
            Student student = (Student) super.clone();

            student.course = (Course) course.clone();
            student.address = (Address) address.clone();
            student.dob = (Date) dob.clone();

            return student;
        }
    }

答案 1 :(得分:0)

要进行浅层复制,只需将一个变量设置为另一个变量即可。浅拷贝意味着原始副本和副本实际上是同一个对象。如果你有:

course2 = course1;
course2.name = "math";

然后course1的名称也将更改为“math”,因为course1和course2是同一个对象。

要进行深度克隆,您必须复制对象中的所有内容,而不是对象本身。

course2 = new Course();
course2.name = course1.name;
//... 
course2.name = "math";

在这种情况下,course1的名称不会更改,因为course2本身是作为新对象创建的,并且未设置为course1

答案 2 :(得分:0)

ShallowCopy 中,仅复制参考值,因此如果对其进行任何更改,将直接影响该对象的原始副本。

但是在 DeepCopy 中,您必须创建该对象的新实例,并使用原始对象的值对其进行初始化,并返回新初始化的对象。

您必须在clone方法中执行此操作。因此,当克隆被称为深度复制时。