我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

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

10年积累的成都网站建设、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有镇安免费网站建设让你可以放心的选择与我们合作。

序列化的方法简单,需要将所有涉及到的类实现接口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详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


新闻标题:浅谈Java中实现深拷贝的两种方式—clone()&Serialized
标题路径:http://shouzuofang.com/article/jghehc.html

其他资讯