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

网友投稿 318 2023-01-11


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

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.nFXHZGVsetcar(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(clonhttp://ed_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())http://;

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:桌面端软件接口测试(软件接口测试实战详解)
下一篇:Java for循环详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~