服务器之家

服务器之家 > 正文

浅谈Java中实现深拷贝的两种方式—clone() & Serialized

时间:2019-06-21 14:49     来源/作者:刘知安

clone() 方法麻烦一些,需要将所有涉及到的类实现声明式接口 Cloneable,并覆盖Object类中的clone()方法,并设置作用域为public(这是为了其他类可以使用到该clone方法)。

序列化的方法简单,需要将所有涉及到的类实现接口Serializable

package b1ch06.clone;

import java.io.Serializable;

class Car implements Cloneable, Serializable {
  private String band;

  public Car(String band) {
    this.band = band;
  }

  public String getBand() {
    return band;
  }

  public void setBand(String band) {
    this.band = band;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
}
package b1ch06.clone;

import java.io.Serializable;

class Employee implements Cloneable, Serializable {
  private String name;
  private Car car;

  public Employee(String name, Car car) {
    this.name = name;
    this.car = car;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Car getcar() {
    return car;
  }

  public void setcar(Car car) {
    this.car = car;
  }

  protected void test() {
    System.out.println("test func");
  }

  @Override
  public Object clone() throws CloneNotSupportedException {

    Employee employee_cloned = (Employee) super.clone();
    Car car_cloned = (Car) this.car.clone();
    employee_cloned.setcar(car_cloned);
    return employee_cloned;
  }
}


package b1ch06.clone;


import java.io.*;


public class SerializedClone {
  @SuppressWarnings("unchecked")
  public static <T extends Serializable> T clone(T obj) {
    T cloneObj = null;
    try {
      //写入字节流
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ObjectOutputStream obs = new ObjectOutputStream(out);
      obs.writeObject(obj);
      obs.close();

      //分配内存,写入原始对象,生成新对象
      ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
      ObjectInputStream ois = new ObjectInputStream(ios);
      //返回生成的新对象
      cloneObj = (T) ois.readObject();
      ois.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return cloneObj;
  }


}
package b1ch06.clone;

public class MyClone {


  public static void main(String[] args) {
    Car car = new Car("BMW");
    Employee employee = new Employee("ANDY", car);
    // 方法一:覆盖所有涉及到的类的clone()方法
    try {

      Employee employee_cp = (Employee) employee.clone();

      System.out.println("=========================");
      System.out.println("original对象地址?:");
      System.out.println(employee.toString());
      System.out.println("copy对象地址?:");
      System.out.println(employee_cp.toString());
      System.out.println("前后两个对象指向同一地址?:");
      System.out.println(employee_cp == employee);
      System.out.println("=========================");

      System.out.println("original对象中car对象地址?:");
      System.out.println(employee.getcar().toString());
      System.out.println("copy对象中car对象地址?:");
      System.out.println(employee_cp.getcar().toString());
      System.out.println("前后两个car对象指向同一地址?:");
      System.out.println(employee_cp == employee);

    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }

    // 方法二:序列化实现深拷贝
    Employee cloned_employee = SerializedClone.clone(employee);
    System.out.println("=========================");
    System.out.println("original对象地址?:");
    System.out.println(employee.toString());
    System.out.println("copy对象地址?:");
    System.out.println(cloned_employee.toString());
    System.out.println("前后两个对象指向同一地址?:");
    System.out.println(cloned_employee == employee);

    System.out.println("=========================");

    System.out.println("original对象中car对象地址?:");
    System.out.println(employee.getcar().toString());
    System.out.println("copy对象中car对象地址?:");
    System.out.println(cloned_employee.getcar().toString());
    System.out.println("前后两个car对象指向同一地址?:");
    System.out.println(cloned_employee == employee);

  }
}

以上所述是小编给大家介绍的Java中实现深拷贝的两种方式--——clone() & Serialized详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

相关文章

热门资讯

叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍
叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍 2019-06-05
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
淘宝合合卡可以送好友吗 淘宝合合卡快速获得方式
淘宝合合卡可以送好友吗 淘宝合合卡快速获得方式 2019-06-13
叠猫猫可以邀请多少人 叠猫猫大挑战一个队最多多少人
叠猫猫可以邀请多少人 叠猫猫大挑战一个队最多多少人 2019-06-05
淘宝618叠猫猫怎么退出队伍?每天可退出几次队伍
淘宝618叠猫猫怎么退出队伍?每天可退出几次队伍 2019-06-06
返回顶部