最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年03月14日 14:25:08   作者:劉知安  
這篇文章主要介紹了Java中實(shí)現(xiàn)深拷貝的兩種方式—clone() & Serialized,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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

序列化的方法簡(jiǎn)單,需要將所有涉及到的類實(shí)現(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)存,寫入原始對(duì)象,生成新對(duì)象
      ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
      ObjectInputStream ois = new ObjectInputStream(ios);
      //返回生成的新對(duì)象
      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對(duì)象地址?:");
      System.out.println(employee.toString());
      System.out.println("copy對(duì)象地址?:");
      System.out.println(employee_cp.toString());
      System.out.println("前后兩個(gè)對(duì)象指向同一地址?:");
      System.out.println(employee_cp == employee);
      System.out.println("=========================");

      System.out.println("original對(duì)象中car對(duì)象地址?:");
      System.out.println(employee.getcar().toString());
      System.out.println("copy對(duì)象中car對(duì)象地址?:");
      System.out.println(employee_cp.getcar().toString());
      System.out.println("前后兩個(gè)car對(duì)象指向同一地址?:");
      System.out.println(employee_cp == employee);

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

    // 方法二:序列化實(shí)現(xiàn)深拷貝
    Employee cloned_employee = SerializedClone.clone(employee);
    System.out.println("=========================");
    System.out.println("original對(duì)象地址?:");
    System.out.println(employee.toString());
    System.out.println("copy對(duì)象地址?:");
    System.out.println(cloned_employee.toString());
    System.out.println("前后兩個(gè)對(duì)象指向同一地址?:");
    System.out.println(cloned_employee == employee);

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

    System.out.println("original對(duì)象中car對(duì)象地址?:");
    System.out.println(employee.getcar().toString());
    System.out.println("copy對(duì)象中car對(duì)象地址?:");
    System.out.println(cloned_employee.getcar().toString());
    System.out.println("前后兩個(gè)car對(duì)象指向同一地址?:");
    System.out.println(cloned_employee == employee);

  }
}

以上所述是小編給大家介紹的Java中實(shí)現(xiàn)深拷貝的兩種方式--——clone() & Serialized詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

临夏县| 上杭县| 南康市| 汤阴县| 杭锦后旗| 洛川县| 葫芦岛市| 额尔古纳市| 怀安县| 襄汾县| 通州区| 澜沧| 鄱阳县| 新龙县| 衡阳市| 临夏县| 阿拉善右旗| 台东县| 丘北县| 土默特左旗| 伊宁市| 徐州市| 东乡| 五台县| 平湖市| 富裕县| 屏南县| 涞水县| 临汾市| 乌拉特后旗| 留坝县| 晋宁县| 永济市| 页游| 托克托县| 鸡东县| 晋中市| 山西省| 博爱县| 逊克县| 师宗县|