clone() 方法麻煩一些,需要將所有涉及到的類實現聲明式接口 Cloneable,并覆蓋Object類中的clone()方法,并設置作用域為public(這是為了其他類可以使用到該clone方法)。
序列化的方法簡單,需要將所有涉及到的類實現接口Serializable
03 | import java.io.Serializable; |
05 | class Car implements Cloneable, Serializable { |
08 | public Car(String band) { |
12 | public String getBand() { |
16 | public void setBand(String band) { |
21 | public Object clone() throws CloneNotSupportedException { |
03 | import java.io.Serializable; |
05 | class Employee implements Cloneable, Serializable { |
09 | public Employee(String name, Car car) { |
14 | public String getName() { |
18 | public void setName(String name) { |
26 | public void setcar(Car car) { |
30 | protected void test() { |
31 | System.out.println( "test func" ); |
35 | public Object clone() throws CloneNotSupportedException { |
37 | Employee employee_cloned = (Employee) super .clone(); |
38 | Car car_cloned = (Car) this .car.clone(); |
39 | employee_cloned.setcar(car_cloned); |
40 | return employee_cloned; |
07 | public class SerializedClone { |
08 | @SuppressWarnings ( "unchecked" ) |
09 | public static <T extends Serializable> T clone(T obj) { |
13 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
14 | ObjectOutputStream obs = new ObjectOutputStream(out); |
19 | ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray()); |
20 | ObjectInputStream ois = new ObjectInputStream(ios); |
22 | cloneObj = (T) ois.readObject(); |
24 | } catch (Exception e) { |
06 | public static void main(String[] args) { |
07 | Car car = new Car( "BMW" ); |
08 | Employee employee = new Employee( "ANDY" , car); |
12 | Employee employee_cp = (Employee) employee.clone(); |
14 | System.out.println( "=========================" ); |
15 | System.out.println( "original對象地址?:" ); |
16 | System.out.println(employee.toString()); |
17 | System.out.println( "copy對象地址?:" ); |
18 | System.out.println(employee_cp.toString()); |
19 | System.out.println( "前后兩個對象指向同一地址?:" ); |
20 | System.out.println(employee_cp == employee); |
21 | System.out.println( "=========================" ); |
23 | System.out.println( "original對象中car對象地址?:" ); |
24 | System.out.println(employee.getcar().toString()); |
25 | System.out.println( "copy對象中car對象地址?:" ); |
26 | System.out.println(employee_cp.getcar().toString()); |
27 | System.out.println( "前后兩個car對象指向同一地址?:" ); |
28 | System.out.println(employee_cp == employee); |
30 | } catch (CloneNotSupportedException e) { |
35 | Employee cloned_employee = SerializedClone.clone(employee); |
36 | System.out.println( "=========================" ); |
37 | System.out.println( "original對象地址?:" ); |
38 | System.out.println(employee.toString()); |
39 | System.out.println( "copy對象地址?:" ); |
40 | System.out.println(cloned_employee.toString()); |
41 | System.out.println( "前后兩個對象指向同一地址?:" ); |
42 | System.out.println(cloned_employee == employee); |
44 | System.out.println( "=========================" ); |
46 | System.out.println( "original對象中car對象地址?:" ); |
47 | System.out.println(employee.getcar().toString()); |
48 | System.out.println( "copy對象中car對象地址?:" ); |
49 | System.out.println(cloned_employee.getcar().toString()); |
50 | System.out.println( "前后兩個car對象指向同一地址?:" ); |
51 | System.out.println(cloned_employee == employee); |
以上所述是小編給大家介紹的Java中實現深拷貝的兩種方式--——clone() & Serialized詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!