溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

淺談Java中實現(xiàn)深拷貝的兩種方式—clone() & Serialized

發(fā)布時間:2020-09-02 14:05:17 來源:腳本之家 閱讀:208 作者:劉知安 欄目:編程語言

clone() 方法麻煩一些,需要將所有涉及到的類實現(xiàn)聲明式接口 Cloneable,并覆蓋Object類中的clone()方法,并設(shè)置作用域為public(這是為了其他類可以使用到該clone方法)。

序列化的方法簡單,需要將所有涉及到的類實現(xiàn)接口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 {
      //寫入字節(jié)流
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ObjectOutputStream obs = new ObjectOutputStream(out);
      obs.writeObject(obj);
      obs.close();

      //分配內(nèi)存,寫入原始對象,生成新對象
      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();
    }

    // 方法二:序列化實現(xiàn)深拷貝
    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中實現(xiàn)深拷貝的兩種方式--——clone() & Serialized詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI