2017-04-21 129 views
0

我有一个包内的三个类pack1。这三个类别是classAclassBclassC如何在java中进行浅层克隆和深度克隆?

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; 

    } 

,我想知道我深克隆地址,出生日期和浅克隆过程?我不知道如何做克隆的组合

回答

1

要做一个浅拷贝,你只需设置一个变量等于另一个。浅拷贝意味着原始文件和副本确实是同一个对象。如果您有:

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

然后course1的名称也将变更为‘数学’,因为course1和course2是同一个对象。

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

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

在这种情况下,course1的名字不会改变,因为course2本身做成一个新的对象,并没有设置为course1

+0

赋值'course2 = course1'不是浅拷贝,只是参考。浅或深是指管理复制对象的对象属性的方式 –

1

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

深度复制: 对象的深层副本将具有原始对象的所有字段的精确副本,就像浅拷贝一样。但是另外,如果原始对象将任何其他对象引用为字段,那么通过调用它们的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; 
     } 
    } 
0

ShallowCopy仅供参考值被复制,所以如果你把它的任何变化将直接影响到该对象的原始副本。

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

您必须在克隆方法中执行此操作。所以当克隆被称为深层拷贝被执行时。