【问题标题】:How to shallow clone and deep clone in java?如何在java中进行浅克隆和深度克隆?
【发布时间】:2017-09-17 22:50:39
【问题描述】:

我在包pack1 中有三个类。这三个类是classAclassB和classC。

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以及浅克隆课程的?我还不知道怎么做克隆的组合

【问题讨论】:

    标签: java cloning


    【解决方案1】:

    在ShallowCopy 中只复制引用值,因此如果您对其进行任何更改将直接影响该对象的原始副本。

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

    您必须在 clone 方法中执行此操作。这样当调用 clone 时就会执行 deepcopy。

    【讨论】:

      【解决方案2】:

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

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

      由于课程实体没有任何对象引用,因此使用默认克隆方法对其进行克隆。 学生实体作为 Date 、 Course 、 Address 的引用,我们需要重写 clone 方法。 下面是示例代码:-

      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;
              }
          }
      

      【讨论】:

        【解决方案3】:

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

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

        那么course1 的名字也会变成“math”,因为 course1 和 course2 是同一个对象。

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

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

        在这种情况下,course1 的名称不会改变,因为 course2 本身是作为一个新对象制作的,并且没有设置为 course1。

        【讨论】:

        • 赋值course2 = course1 不是浅拷贝,只是参考。浅或深是指复制对象的对象属性的管理方式
        猜你喜欢
        • 1970-01-01
        • 2013-10-11
        • 2013-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-24
        • 1970-01-01
        • 2011-09-05
        相关资源
        最近更新 更多