日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用序列化实现对象的拷贝(转载)

發(fā)布時(shí)間:2024/1/17 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用序列化实现对象的拷贝(转载) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章出處:http://www.cnblogs.com/chenssy/p/3382979.html

實(shí)現(xiàn)Cloneable接口:實(shí)現(xiàn)該接口的類都會(huì)具備被拷貝的能力,同時(shí)拷貝是在內(nèi)存中進(jìn)行,在性能方面比我們直接通過new生成對(duì)象來的快,特別是在大對(duì)象的生成上,使得性能的提升非常明顯。

?

1 package com.itinfo.depthcopy; 2 3 /** 4 * @author Wáng Chéng Dá 5 * @create 2017-02-24 9:14 6 */ 7 public class Email { 8 9 private String type; 10 private String content; 11 12 public Email(String content) { 13 this.content = content; 14 } 15 16 public Email(String type, String content) { 17 this.type = type; 18 this.content = content; 19 } 20 21 public Email() { 22 } 23 24 public String getType() { 25 return type; 26 } 27 28 public void setType(String type) { 29 this.type = type; 30 } 31 32 public String getContent() { 33 return content; 34 } 35 36 public void setContent(String content) { 37 this.content = content; 38 } 39 40 @Override 41 public String toString() { 42 return "Email{" + 43 "type='" + type + '\'' + 44 ", content='" + content + '\'' + 45 '}'; 46 } 47 48 }

?

淺拷貝:

1 package com.itinfo.depthcopy; 2 3 /** 4 * 實(shí)體類 5 * 6 * @author Wáng Chéng Dá 7 * @create 2017-02-24 14:53 8 */ 9 public class Person implements Cloneable { 10 11 /** 姓名 **/ 12 private String name; 13 14 /** 電子郵件 **/ 15 private Email email; 16 17 public Person() { 18 } 19 20 public Person(String name, Email email) { 21 this.name = name; 22 this.email = email; 23 } 24 25 public Person(String name) { 26 this.name = name; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public Email getEmail() { 38 return email; 39 } 40 41 public void setEmail(Email email) { 42 this.email = email; 43 } 44 45 protected Person clone() { 46 Person person = null; 47 try { 48 person = (Person)super.clone(); 49 // person.setEmail(new Email(person.getEmail().getType(),person.getEmail().getContent()));//在堆內(nèi)存中重新分配一塊內(nèi)存存放新生成的email,給屬性賦值 50 }catch (CloneNotSupportedException e) { 51 e.printStackTrace(); 52 } 53 return person; 54 } 55 } 1 package com.itinfo.depthcopy; 2 3 /** 4 * 深拷貝測(cè)試 5 * 6 * @author Wáng Chéng Dá 7 * @create 2017-02-24 9:12 8 */ 9 public class Client { 10 public static void main(String[] args) { 11 //寫封郵件 12 Email email = new Email("請(qǐng)參加會(huì)議","請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議..."); 13 14 Person person1 = new Person("張三",email); 15 16 Person person2 = person1.clone(); 17 person2.setName("李四"); 18 Person person3 = person1.clone(); 19 person3.setName("王五"); 20 21 System.out.println(person1.getName() + "的郵件內(nèi)容是:" + person1.getEmail().getContent()); 22 System.out.println(person2.getName() + "的郵件內(nèi)容是:" + person2.getEmail().getContent()); 23 System.out.println(person3.getName() + "的郵件內(nèi)容是:" + person3.getEmail().getContent()); 24 25 person1.getEmail().setContent("請(qǐng)與今天12:00到二會(huì)議室參加會(huì)議..."); 26 27 System.out.println("------------修改張三的郵件內(nèi)容-------------"); 28 29 System.out.println(person1.getName() + "的郵件內(nèi)容是:" + person1.getEmail().getContent()); 30 System.out.println(person2.getName() + "的郵件內(nèi)容是:" + person2.getEmail().getContent()); 31 System.out.println(person3.getName() + "的郵件內(nèi)容是:" + person3.getEmail().getContent()); 32 } 33 }

??

控制臺(tái)輸出:

張三的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
李四的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
王五的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
------------修改張三的郵件內(nèi)容-------------
張三的郵件內(nèi)容是:請(qǐng)與今天12:00到二會(huì)議室參加會(huì)議...
李四的郵件內(nèi)容是:請(qǐng)與今天12:00到二會(huì)議室參加會(huì)議...
王五的郵件內(nèi)容是:請(qǐng)與今天12:00到二會(huì)議室參加會(huì)議...

?

深拷貝:

把Person中的?person.setEmail(new Email(person.getEmail().getType(),person.getEmail().getContent())); 注釋放開。

?

控制臺(tái)輸出:

張三的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
李四的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
王五的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
------------修改張三的郵件內(nèi)容-------------
張三的郵件內(nèi)容是:請(qǐng)與今天12:00到二會(huì)議室參加會(huì)議...
李四的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
王五的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...

?

利用序列化實(shí)現(xiàn)對(duì)象的拷貝:

? ? ? ?如何利用序列化來完成對(duì)象的拷貝呢?在內(nèi)存中通過字節(jié)流的拷貝是比較容易實(shí)現(xiàn)的。把母對(duì)象寫入到一個(gè)字節(jié)流中,再從字節(jié)流中將其讀出來,這樣就可以創(chuàng)建一個(gè)新的對(duì)象了,并且該新對(duì)象與母對(duì)象之間并不存在引用共享的問題,真正實(shí)現(xiàn)對(duì)象的深拷貝。

1 package com.itinfo.IOcopy; 2 3 import java.io.*; 4 5 /** 6 * IO序列化拷貝 7 * 8 * @author Wáng Chéng Dá 9 * @create 2017-02-24 15:23 10 */ 11 public class CloneUtils { 12 @SuppressWarnings("unchecked") 13 public static <T extends Serializable> T clone(T obj){ 14 T cloneObj = null; 15 try { 16 //寫入字節(jié)流 17 ByteArrayOutputStream out = new ByteArrayOutputStream(); 18 ObjectOutputStream obs = new ObjectOutputStream(out); 19 obs.writeObject(obj); 20 obs.close(); 21 22 //分配內(nèi)存,寫入原始對(duì)象,生成新對(duì)象 23 ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray()); 24 ObjectInputStream ois = new ObjectInputStream(ios); 25 //返回生成的新對(duì)象 26 cloneObj = (T) ois.readObject(); 27 ois.close(); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 return cloneObj; 32 } 33 }

?

使用該工具類的對(duì)象必須要實(shí)現(xiàn)Serializable接口,否則是沒有辦法實(shí)現(xiàn)克隆的。

1 package com.itinfo.IOcopy; 2 3 4 import java.io.Serializable; 5 6 /** 7 * 實(shí)體類 8 * 9 * @author Wáng Chéng Dá 10 * @create 2017-02-24 14:53 11 */ 12 public class Person implements Serializable { 13 14 private static final long serialVersionUID = -8436289855400551515L; 15 /** 姓名 **/ 16 private String name; 17 18 /** 電子郵件 **/ 19 private Email email; 20 21 public Person() { 22 } 23 24 public Person(String name, Email email) { 25 this.name = name; 26 this.email = email; 27 } 28 29 public Person(String name) { 30 this.name = name; 31 } 32 33 34 35 public String getName() { 36 return name; 37 } 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 public Email getEmail() { 44 return email; 45 } 46 47 public void setEmail(Email email) { 48 this.email = email; 49 } 50 51 }

?

1 package com.itinfo.IOcopy; 2 3 import java.io.Serializable; 4 5 /** 6 * @author Wáng Chéng Dá 7 * @create 2017-02-24 9:14 8 */ 9 public class Email implements Serializable { 10 11 private static final long serialVersionUID = 1820313496253067610L; 12 private String type; 13 private String content; 14 15 public Email(String content) { 16 this.content = content; 17 } 18 19 public Email(String type, String content) { 20 this.type = type; 21 this.content = content; 22 } 23 24 public Email() { 25 } 26 27 public String getType() { 28 return type; 29 } 30 31 public void setType(String type) { 32 this.type = type; 33 } 34 35 public String getContent() { 36 return content; 37 } 38 39 public void setContent(String content) { 40 this.content = content; 41 } 42 43 @Override 44 public String toString() { 45 return "Email{" + 46 "type='" + type + '\'' + 47 ", content='" + content + '\'' + 48 '}'; 49 } 50 51 } 1 package com.itinfo.IOcopy; 2 3 /** 4 * 深拷貝測(cè)試 5 * 6 * @author Wáng Chéng Dá 7 * @create 2017-02-24 9:12 8 */ 9 public class Client { 10 public static void main(String[] args) { 11 //寫封郵件 12 Email email = new Email("請(qǐng)參加會(huì)議", "請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議..."); 13 14 Person person1 = new Person("張三", email); 15 16 Person person2 = CloneUtils.clone(person1); 17 person2.setName("李四"); 18 Person person3 = CloneUtils.clone(person1); 19 person3.setName("王五"); 20 21 System.out.println(person1.getName() + "的郵件內(nèi)容是:" + person1.getEmail().getContent()); 22 System.out.println(person2.getName() + "的郵件內(nèi)容是:" + person2.getEmail().getContent()); 23 System.out.println(person3.getName() + "的郵件內(nèi)容是:" + person3.getEmail().getContent()); 24 25 person1.getEmail().setContent("請(qǐng)與今天12:00到二會(huì)議室參加會(huì)議..."); 26 27 System.out.println("------------修改張三的郵件內(nèi)容-------------"); 28 29 System.out.println(person1.getName() + "的郵件內(nèi)容是:" + person1.getEmail().getContent()); 30 System.out.println(person2.getName() + "的郵件內(nèi)容是:" + person2.getEmail().getContent()); 31 System.out.println(person3.getName() + "的郵件內(nèi)容是:" + person3.getEmail().getContent()); 32 } 33 }

?

控制臺(tái)輸出:

張三的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
李四的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
王五的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
------------修改張三的郵件內(nèi)容-------------
張三的郵件內(nèi)容是:請(qǐng)與今天12:00到二會(huì)議室參加會(huì)議...
李四的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...
王五的郵件內(nèi)容是:請(qǐng)與今天12:30到二會(huì)議室參加會(huì)議...

轉(zhuǎn)載于:https://www.cnblogs.com/goodcheap/p/6438919.html

總結(jié)

以上是生活随笔為你收集整理的使用序列化实现对象的拷贝(转载)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。