物件的克隆
物件的克隆
物件地址的引用:
public class Person { int id; String name; public Person(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "編號:"+ this.id+" 姓名:"+ this.name; } } public class Demo { public static void main(String[] args) throws Exception { Person p1 = new Person(123,"hcx"); Person p2 = p1; System.out.println("p1:"+p1);//p1:編號:123 姓名:hcx System.out.println("p2:"+ p2);//p2:編號:123 姓名:hcx } }
以上屬於物件地址的引用:

物件地址的引用.png
一、物件的淺克隆
Person:
class Address{ String city; public Address(String city){ this.city = city; } } public class Person implements Cloneable{ int id; String name; Address address; public Person(int id, String name) { this.id = id; this.name = name; } public Person(int id, String name, Address address) { this.id = id; this.name = name; this.address = address; System.out.println("=======構造方法呼叫了==="); } @Override public String toString() { return "編號:"+ this.id+" 姓名:"+ this.name+" 地址:"+ address.city; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } }
Demo:
public class Demo { public static void main(String[] args) throws Exception { Address address = new Address("深圳"); Person p1 = new Person(123,"hcx",address); Person p2 = (Person) p1.clone(); //clone() 克隆了一個物件。 p2.name = "gia"; p2.address.city ="廣州"; System.out.println("p1:"+p1); System.out.println("p2:"+ p2); System.out.println("p1:"+p1);//p1:編號:123 姓名:hcx 地址:廣州 System.out.println("p2:"+ p2);//p2:編號:123 姓名:gia 地址:廣州 } }

物件的淺克隆.png
物件淺克隆要注意的細節:
- 如果一個物件需要呼叫clone的方法克隆,那麼該物件所屬的類必須要實現Cloneable介面。
- Cloneable介面是一個標識介面而已,沒有任何方法。
- 物件的淺克隆就是克隆一個物件的時候,如果被克隆的物件中維護了另外一個類的物件,這時候只是克隆另外一個物件的地址,而沒有把另外一個物件也克隆一份。
- 物件的淺克隆不會呼叫到構造方法的。
二、物件的深克隆
物件的深克隆:利用物件的輸入輸出流把物件先寫到檔案上,然後再讀取物件的資訊。這個過程就稱作為物件的深克隆。
Person:
import java.io.Serializable; class Address implements Serializable{ String city; public Address(String city){ this.city = city; } } public class Person implements Cloneable,Serializable { int id; String name; Address address; public Person(int id, String name) { this.id = id; this.name = name; } public Person(int id, String name, Address address) { this.id = id; this.name = name; this.address = address; System.out.println("=======構造方法呼叫了==="); } @Override public String toString() { return "編號:"+ this.id+" 姓名:"+ this.name+" 地址:"+ address.city; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } }
Demo2:
public class Demo2 { public static void main(String[] args) throws IOException, ClassNotFoundException { Address address = new Address("深圳"); Person p1 = new Person(123,"hcx",address); writeObj(p1); Person p2=readObj(); p2.address.city = "廣州"; System.out.println("p1:"+ p1);//p1:編號:123 姓名:hcx 地址:深圳 System.out.println("p2:"+ p2);//p1:編號:123 姓名:hcx 地址:廣州 } //再從檔案中讀取物件的資訊 public static Person readObj() throws ClassNotFoundException, IOException{ FileInputStream fileInputStream = new FileInputStream("F:\\obj.txt"); //建立物件的輸入流物件 ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); return (Person) objectInputStream.readObject(); } //先要把物件寫到檔案上。 public static void writeObj(Person p) throws IOException{ //建立一個檔案 的輸出流物件 FileOutputStream fileOutputStream= new FileOutputStream("F:\\obj.txt"); //建立物件的輸出流 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); //把物件寫出 objectOutputStream.writeObject(p); //關閉資源 objectOutputStream.close(); } }