這篇文章主要介紹了java對(duì)象中淺克隆和深克隆的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括武都網(wǎng)站建設(shè)、武都網(wǎng)站制作、武都網(wǎng)頁(yè)制作以及武都網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,武都網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到武都省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
引言:
在Object基類中,有一個(gè)方法叫clone,產(chǎn)生一個(gè)前期對(duì)象的克隆,克隆對(duì)象是原對(duì)象的拷貝,由于引用類型的存在,有深克隆和淺克隆之分,若克隆對(duì)象中存在引用類型的屬性,深克隆會(huì)將此屬性完全拷貝一份,而淺克隆僅僅是拷貝一份此屬性的引用。首先看一下容易犯的幾個(gè)小問(wèn)題
clone方法是Object類的,并不是Cloneable接口的,Cloneable只是一個(gè)標(biāo)記接口,標(biāo)記接口是用用戶標(biāo)記實(shí)現(xiàn)該接口的類具有某種該接口標(biāo)記的功能,常見(jiàn)的標(biāo)記接口有三個(gè):Serializable、Cloneable、RandomAccess,沒(méi)有實(shí)現(xiàn)Cloneable接口,那么調(diào)用clone方法就會(huì)爆出CloneNotSupportedException異常。
Object類中的clone方法是protected修飾的,這就表明我們?cè)谧宇愔胁恢貙懘朔椒?,就在子類外無(wú)法訪問(wèn),因?yàn)檫@個(gè)protected權(quán)限是僅僅能在Object所在的包和子類能訪問(wèn)的,這也驗(yàn)證了子類重寫父類方法權(quán)限修飾符可以變大但不能變小的說(shuō)法。
protected native Object clone() throws CloneNotSupportedException;
重寫clone方法,內(nèi)部?jī)H僅是調(diào)用了父類的clone方法,其實(shí)是為了擴(kuò)大訪問(wèn)權(quán)限,當(dāng)然你可以把protected改為public,以后再繼承就不用重寫了。當(dāng)然只是淺克隆的clone函數(shù),深克隆就需要修改了。
@Override protected Object clone() throws CloneNotSupportedException { return super.clone(); }
屬性是String的情況,String也是一個(gè)類,那String引用類型嗎?String的表現(xiàn)有的像基本類型,歸根到底就是因?yàn)镾tring不可改變,克隆之后倆個(gè)引用指向同一個(gè)String,但當(dāng)修改其中的一個(gè),改的不是String的值,卻是新生成一個(gè)字符串,讓被修改的引用指向新的字符串。外表看起來(lái)就像基本類型一樣。
淺克?。?/strong>
淺克隆就是引用類型的屬性無(wú)法完全復(fù)制,類User中包含成績(jī)屬性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); } }
輸出結(jié)果為:
原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也修改了。而要想不被影響,就需要深克隆了。
深克隆:
方式一:clone函數(shù)的嵌套調(diào)用
既然引用類型無(wú)法被完全克隆,那將引用類型也實(shí)現(xiàn)Cloneable接口重寫clone方法,在User類中的clone方法調(diào)用屬性的克隆方法,也就是方法的嵌套調(diào)用
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); } }
輸出結(jié)果為:
原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=99}}
方式二:序列化
上一種方法已經(jīng)足夠滿足我們的需要,但是如果類之間的關(guān)系很多,或者是有的屬性是數(shù)組呢,數(shù)組可無(wú)法實(shí)現(xiàn)Cloneable接口(我們可以在clone方法中手動(dòng)復(fù)制數(shù)組),但是每次都得手寫clone方法,很麻煩,而序列化方式只需要給每個(gè)類都實(shí)現(xiàn)一個(gè)Serializable接口,也是標(biāo)記接口,最后同序列化和反序列化操作達(dá)到克隆的目的(包括數(shù)組的復(fù)制)。序列化和反序列化的知識(shí)請(qǐng)參照下一篇
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); } }
輸出結(jié)果:
原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=99}}
帶數(shù)組屬性的克隆
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); } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“java對(duì)象中淺克隆和深克隆的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
網(wǎng)頁(yè)題目:java對(duì)象中淺克隆和深克隆的示例分析
文章出自:http://aaarwkj.com/article30/psoeso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站策劃、關(guān)鍵詞優(yōu)化、定制網(wǎng)站、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)