Java实现深拷贝的两种方式 浅谈Java中实现深拷贝的两种方式—clone() & Serialized
刘知安 人气:0clone() 方法麻烦一些,需要将所有涉及到的类实现声明式接口 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详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
加载全部内容