当前位置:首页 > 开发 > 编程语言 > Java > 正文

java clone

发表于: 2011-12-04   作者:coolxing   来源:转载   浏览:
摘要: [coolxing按: 转载请注明作者和出处, 如有谬误, 欢迎在评论中指正.]   clone用在什么场合下 1. 创建一个新的对象需要很复杂的步骤(例如类的继承层次很深), 而刚好有一个适合的对象用来clone, 这时使用clone技术有助于性能优化; 2. 用在原型(prototype)模式下.   shallow copy和deep copy 两者本质上的

[coolxing按: 转载请注明作者和出处, 如有谬误, 欢迎在评论中指正.]

 

clone用在什么场合下

1. 创建一个新的对象需要很复杂的步骤(例如类的继承层次很深), 而刚好有一个适合的对象用来clone, 这时使用clone技术有助于性能优化;

2. 用在原型(prototype)模式下.

 

shallow copydeep copy

两者本质上的区别在于, 采用shallow copy克隆得到的对象和被克隆对象的引用属性指向相同的地址. 而采用deep copy克隆得到的对象和被克隆对象的引用属性指向不同的地址.

采用shallow copy的方式克隆一个对象时, 如果对象中包含引用属性, 那么直接复制一份引用给新对象的对应属性. deep copy则会为新对象的引用属性开辟新的内存空间然后再将地址赋值给引用.

这里的引用属性泛指除基本数据类型, 包装数据类型, String类型之外的属性.

Object类的clone方法是一个native方法, 属于shallow copy的范畴. 调用该方法时首先会判断类是否实现了Cloneable接口, 如果没有实现Cloneable接口, 会抛出CloneNotSupportedException异常.

 

调用Object类的clone方法实现shallow copy的例子:

public class Person implements Cloneable {
   private int age;
   private String name;
   private Address address;
 
   public Person(int age, String name, Address address) {
      this.age = age;
      this.name = name;
      this.address = address;
   }
 
   @Override
   public Person clone() throws CloneNotSupportedException {
      // 直接调用Object类的clone方法实现的是shallow copy
      return (Person) super.clone();
   }
 
   public static void main(String[] args) {
      Person old = new Person(24, "coolxing", new Address("beijing"));
      try {
         Person newPerson = old.clone();
         System.out.println("没有更改newPerson的属性时, newPerson.name = "
                + newPerson.name + ", newPerson.age = " + newPerson.age
                + ", newPerson.address.getCity() = "
                + newPerson.address.getCity());
         System.out.println("没有更改newPerson的属性时, old.name = " + old.name
                + ", old.age = " + old.age + ", old.address.getCity() = "
                + old.address.getCity());
         newPerson.name = "min";
         newPerson.age = 22;
         newPerson.address.setCity("dalian");
         System.out.println("更改了newPerson的属性时, newPerson.name = "
                + newPerson.name + ", newPerson.age = " + newPerson.age
                + ", newPerson.address.getCity() = "
                + newPerson.address.getCity());
         System.out.println("更改了newPerson的属性时, old.name = " + old.name
                + ", old.age = " + old.age + ", old.address.getCity() = "
                + old.address.getCity());
      } catch (CloneNotSupportedException e) {
         e.printStackTrace();
      }
   }
}
 
public class Address {
   private String city;
   public Address(String city) {
      this.city = city;
   }
   public String getCity() {
      return city;
   }
   public void setCity(String city) {
      this.city = city;
   }
}

程序输出:

没有更改newPerson的属性时, newPerson.name = coolxing, newPerson.age = 24, newPerson.address.getCity() = beijing

没有更改newPerson的属性时, old.name = coolxing, old.age = 24, old.address.getCity() = beijing

更改了newPerson的属性时, newPerson.name = min, newPerson.age = 22, newPerson.address.getCity() = dalian

更改了newPerson的属性时, old.name = coolxing, old.age = 24, old.address.getCity() = dalian

由此可见, old对象和newPerson对象的address属性指向同一个Address对象. 更改newPerson对象的address属性的值会影响到old对象.

 

怎样实现deep copy

shallow copy并非一无是处, 也有相应的应用场合. 如果确实是需要deep copy, 那就只能由开发者自己实现. 通常实现deep copy的方式有2:

1. 递归调用引用属性对象的clone方法.

Person类增加新的方法:

public Person deepClone() throws CloneNotSupportedException {
      Person newPerson = clone();
      newPerson.address = address.clone();
      return newPerson;
}

这需要Address类也实现Cloneable接口并覆写clone方法:

public Address clone() throws CloneNotSupportedException {
      return (Address) super.clone();
}

在测试方法中调用deepClone方法代替clone方法, 就会得到输出:

更改了newPerson的属性时, newPerson.name = min, newPerson.age = 22, newPerson.address.getCity() = dalian

更改了newPerson的属性时, old.name = coolxing, old.age = 24, old.address.getCity() = beijing

由此可见正确实现了deep copy, newPerson对象属性的修改不会再影响到old对象.

 

2. 反序列化. 先将对象序列化, 然后再反序列化得到原有对象的拷贝, 这是非常彻底的deep copy. 前提是类实现了Serializable接口.

增加deepCloneSerializable方法:

public Person deepCloneSerializable() throws IOException, ClassNotFoundException {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      ObjectOutputStream obOut = new ObjectOutputStream(buffer);
      obOut.writeObject(this);
      obOut.flush();
      ObjectInputStream obIn = new ObjectInputStream(
            new ByteArrayInputStream(buffer.toByteArray()));
      return (Person) obIn.readObject();
}

这需要Address类也实现Serializable接口. 在测试代码中调用deepCloneSerializable方法, 输出结果同上.

 

java clone

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号