欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-21 08:47:58
clone() 方法麻烦一些,需要将所有涉及到的类实现声明式接口 cloneable,并覆盖object类中的clone()方法,并设置作用域为public(这是为了其他类...

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详解整合,希望对大家有所帮助

上一篇:

下一篇: