這篇文章帶給大家的內容是關於java物件中淺克隆和深克隆的詳細分析(附範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
在Object基底類別中,有一個方法叫做clone,產生一個前期物件的克隆,克隆物件是原始物件的拷貝,由於引用類型的存在,有深克隆和淺克隆之分,若克隆物件中存在引用類型的屬性,深克隆會將此屬性完全拷貝一份,而淺克隆僅是拷貝一份此屬性的引用。首先看一下容易犯的幾個小問題
clone方法是Object類別的,並不是Cloneable接口的,Cloneable只是一個標記接口,標記接口是用用戶標記實現該接口的類具有某種該介面標記的功能,常見的標記接口有三個:Serializable、Cloneable、RandomAccess,沒有實作Cloneable接口,那麼呼叫clone方法就會爆出CloneNotSupportedException異常。
Object類別中的clone方法是protected修飾的,這表示我們在子類別中不重寫此方法,就在子類別外無法訪問,因為這個protected權限是僅僅能在Object所在的套件和子類別能存取的,這也驗證了子類別重寫父類別方法權限修飾符可以變大但不能變小的說法。
protected native Object clone() throws CloneNotSupportedException;
重寫clone方法,內部只是呼叫了父類別的clone方法,其實是為了擴大存取權限,當然你可以把protected改為public,以後再繼承就不用重寫了。當然只是淺克隆的clone函數,深克隆就需要修改了。
@Override protected Object clone() throws CloneNotSupportedException { return super.clone(); }
屬性是String的情況,String也是一個類別,那String引用類型嗎? String的表現有的像基本型,歸根到底是因為String不可改變,克隆之後兩個引用指向同一個String,但當修改其中的一個,改的不是String的值,卻是新產生一個字串,讓被修改的引用指向新的字串。外表看起來就像基本類型一樣。
淺克隆就是引用類型的屬性無法完全複製,類別User中包含成績屬性Mark,Mark是由Chinese和math等等組成的,淺克隆失敗的例子
class Mark{ private int chinese; private int math; public Mark(int chinese, int math) { this.chinese = chinese; this.math = math; } public void setChinese(int chinese) { this.chinese = chinese; } public void setMath(int math) { this.math = math; } @Override public String toString() { return "Mark{" + "chinese=" + chinese + ", math=" + math + '}'; } } public class User implements Cloneable{ private String name; private int age; private Mark mark; public User(String name, int age,Mark mark) { this.name = name; this.age = age; this.mark = mark; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", mark=" + mark + '}'; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Mark mark = new Mark(100,99); User user = new User("user",22,mark); User userClone = (User) user.clone(); System.out.println("原user:"+user); System.out.println("克隆的user:"+userClone); //修改引用类型的mark属性 user.mark.setMath(60); System.out.println("修改后的原user:"+user); System.out.println("修改后的克隆user:"+userClone); } }
輸出結果為:
原user:User{name='user', age=22, mark=Mark{chinese=100, math=99}}
克隆的user:User{name='user', age=22, mark=Mark{chinese=100, math=99}}
修改後的原user:User{name='user', age=22, mark= Mark{chinese=100, math=60}}
修改後的複製user:User{name='user', age=22, mark=Mark{chinese=100, math=60}}
很清楚的看到user的mark更改後,被複製的user也修改了。而要不受影響,就需要深克隆了。
既然引用型別無法被完全克隆,那將引用型別也實作Cloneable介面重寫clone方法,在User類別中的clone方法呼叫屬性的複製方法,也就是方法的巢狀呼叫
class Mark implements Cloneable{ private int chinese; private int math; public Mark(int chinese, int math) { this.chinese = chinese; this.math = math; } public void setChinese(int chinese) { this.chinese = chinese; } public void setMath(int math) { this.math = math; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return "Mark{" + "chinese=" + chinese + ", math=" + math + '}'; } } public class User implements Cloneable{ private String name; private int age; private Mark mark; public User(String name, int age,Mark mark) { this.name = name; this.age = age; this.mark = mark; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", mark=" + mark + '}'; } @Override protected Object clone() throws CloneNotSupportedException { User user = (User) super.clone(); user.mark = (Mark) this.mark.clone(); return user; } public static void main(String[] args) throws CloneNotSupportedException { Mark mark = new Mark(100,99); User user = new User("user",22,mark); User userClone = (User) user.clone(); System.out.println("原user:"+user); System.out.println("克隆的user:"+userClone); //修改引用类型的mark属性 user.mark.setMath(60); System.out.println("修改后的原user:"+user); System.out.println("修改后的克隆user:"+userClone); } }
輸出結果為:
原user:User{name='user', age =22, mark=Mark{chinese=100, math=99}}
克隆的user:User{name='user', age=22, mark=Mark{chinese=100, math=99}}
修改後的原user:User{name='user', age=22, mark=Mark{chinese=100, math=60}}
修改過的複製:User{name='user', ageage =22, mark=Mark{chinese=100, math=99}}
上一個方法已經足夠滿足我們的需要,但是如果類別之間的關係很多,或者是有的屬性是數組呢,數組可無法實現Cloneable接口(我們可以在clone方法中手動複製數組),但是每次都得手寫clone方法,很麻煩,而序列化方式只需要給每個類別都實現一個Serializable接口,也是標記接口,最後同序列化和反序列化操作達到克隆的目的(包括數組的複製)。序列化與反序列化的知識請參考下一篇
import java.io.*; class Mark implements Serializable { private int chinese; private int math; public Mark(int chinese, int math) { this.chinese = chinese; this.math = math; } public void setChinese(int chinese) { this.chinese = chinese; } public void setMath(int math) { this.math = math; } @Override public String toString() { return "Mark{" + "chinese=" + chinese + ", math=" + math + '}'; } } public class User implements Serializable{ private String name; private int age; private Mark mark; public User(String name, int age,Mark mark) { this.name = name; this.age = age; this.mark = mark; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", mark=" + mark + '}'; } public static void main(String[] args) throws IOException, ClassNotFoundException { Mark mark = new Mark(100,99); User user = new User("user",22,mark); ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(user);//序列化 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi = new ObjectInputStream(bi); User userClone = (User) oi.readObject();//反序列化 System.out.println("原user:"+user); System.out.println("克隆的user:"+userClone); user.mark.setMath(59); System.out.println("修改后的原user:"+user); System.out.println("修改后的克隆user:"+userClone); } }
輸出結果:
原user:User{name='user', age=22, mark=Mark{chinese= 100, math=99}}
克隆的user:User{name='user', age=22, mark=Mark{chinese=100, math=99}}
修改過的原始原始碼:User name='user', age=22, mark=Mark{chinese=100, math=60}}
修改過的克隆user:User{name='user', age=22, mark=Mark{chinese= 100, math=99}}
帶數組屬性的複製
import java.io.*; import java.util.Arrays; public class User implements Serializable{ private String name; private int age; private int[] arr; public User(String name, int age, int[] arr) { this.name = name; this.age = age; this.arr = arr; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", arr=" + Arrays.toString(arr) + '}'; } public static void main(String[] args) throws IOException, ClassNotFoundException { int[] arr = {1,2,3,4,5,6}; User user = new User("user",22,arr); ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(user);//序列化 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi = new ObjectInputStream(bi); User userClone = (User) oi.readObject();//反序列化 System.out.println("原user:"+user); System.out.println("克隆的user:"+userClone); user.arr[1] = 9; System.out.println("修改后的原user:"+user); System.out.println("修改后的克隆user:"+userClone); } }
以上是java物件中淺克隆和深克隆的詳細分析(附範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!