热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

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

clone() 方法麻烦一些,需要将所有涉及到的类实现声明式接口 Cloneable,并覆盖Object类中的clone()方法,并设置作用

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 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详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


推荐阅读
author-avatar
superC201114_155
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有