被clone的对象如果有属性是引用型对象,如果不复写clone方法,则clone是浅度clone。即当被clone的对象引用的对象发生改变时,clone的对象也会相应发生改变。clone的对象与被clone的对象不能保持独立。
例如:
package hashcode; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; /** * @author Administrator * */ public class Student implements Cloneable { private int age; private String name; private Student deskmate; public Student getDeskmate() { return deskmate; } public void setDeskmate(Student deskmate) { this.deskmate = deskmate; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [age=" + age + ", name=" + name + ", deskmate="+deskmate + "]"; } public Student(int age, String name) { super(); this.age = age; this.name = name; } public static void main(String[] args) throws CloneNotSupportedException { // clone Student goodStudent = new Student("goodStudent", 12); Student student1 = new Student("liuliu", 10); student1.setDeskmate(goodStudent); Student student2 = (Student) student1.clone(); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] System.out.println(student2); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] // student1's deskmate's name is changed. // student2 has the same reference, student2's deskmate's name is the // same as student 1's. // This is shallow clone. student1.getDeskmate().setName("badStudent"); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=badStudent, deskmate=null]] System.out.println(student2); // Student [age=10, name=liuliu, deskmate=Student [age=12, name=badStudent, deskmate=null]] } }
深度clone则需覆写clone方法:
@Override protected Object clone() throws CloneNotSupportedException { Student s = (Student) super.clone(); Student dm = new Student(); dm.setAge(s.getDeskmate().getAge()); dm.setName(s.getDeskmate().getName()); s.setDeskmate(dm); return s; }
这样测试结果表明clone后的对象与被clone的对象是独立的
public static void main(String[] args) throws CloneNotSupportedException { Student goodStudent = new Student("goodStudent", 12); Student student1 = new Student("liuliu", 10); student1.setDeskmate(goodStudent); Student student2 = (Student) student1.clone(); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] System.out.println(student2); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] // student1's deskmate's name is changed. // student2 has its own reference // This is deep copy. student1.getDeskmate().setName("badStudent"); System.out.println(student1); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=badStudent, deskmate=null]] System.out.println(student2); //result: Student [age=10, name=liuliu, deskmate=Student [age=12, name=goodStudent, deskmate=null]] }
相关推荐
Java提供了两种主要的克隆方式:浅度克隆(Shallow Clone)和深度克隆(Deep Clone)。理解这两种克隆的区别对于优化内存管理和复制复杂对象至关重要。 **浅度克隆(Shallow Clone)** 浅度克隆仅仅复制了对象本身...
在C#编程中,了解深度复制和浅度复制的概念至关重要,因为它们直接影响到对象的复制行为,特别是当处理包含复杂数据结构的对象时。本文将详细解释这两种复制方式,并通过一个实例来展示它们的区别。 首先,让我们...
在Java中,通过实现`Cloneable`接口并覆盖`Object`类的`clone()`方法,可以实现浅拷贝。以下代码展示了浅拷贝的例子: ```java public class ShallowCopy { public static void main(String[] args) { Professor0...
总结起来,"prototypeAndCreate.zip"中的内容可能是关于Java编程中原型模式的实践,包括浅度和深度克隆的实现,以及如何在创建车辆实例时应用这些概念。这些示例代码对于理解原型模式和创建模式在实际开发中的运用...
在文章的实例中,通过实现ICloneable接口的Clone方法,Cloner类被修改为能够创建MyContent成员变量的深拷贝,从而实现真正的深度复制。如果MyContent的成员变量是引用类型,则需要递归地调用克隆方法来完成深度复制...
Java的克隆机制通过`Object.clone()`方法实现,该方法位于`java.lang.Object`类中。 **1.2 实现要求** - **实现`Cloneable`接口**:被克隆的类必须实现`Cloneable`接口,表明该类支持克隆操作。 - **重写`clone()`...
JavaScript中的对象克隆是一个重要的概念...总的来说,JavaScript中的对象克隆是处理复杂数据结构的关键技术,理解浅度克隆和深度克隆的区别以及如何实现它们,可以帮助我们编写更健壮的代码,避免意外地修改原始数据。
2. **实用函数**:包括`_.uniqueId`用于生成唯一ID,`_.clone`用于深度或浅度克隆对象,`_.isEqual`用于比较两个值是否相等,以及`_.throttle`和`_.debounce`用于节流和防抖,优化性能。 3. **对象操作**:...