1 package com.lovo.prototype.two; 2 public class Player implements Cloneable { 3 4 private int id; 5 private String name; 6 private String skill; 7 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public String getSkill() { 21 return skill; 22 } 23 public void setSkill(String skill) { 24 this.skill = skill; 25 } 26 27 28 @Override 29 public String toString() { 30 return "Player [id=" + id + ", name=" + name + ", skill=" + skill + "]"; 31 } 32 33 /** 34 * 浅克隆 35 * @return 36 * @throws CloneNotSupportedException 37 */ 38 public Player qianClone() throws CloneNotSupportedException{ 39 return (Player) super.clone(); 40 } 41 42 43 44 45 46 }
1 package com.lovo.prototype.two; 2 3 public class Test { 4 5 public static void main(String[] args) throws CloneNotSupportedException { 6 Player p=new Player(); 7 p.setId(1); 8 p.setName("小明"); 9 p.setSkill("灌篮"); 10 11 Player p2=p.qianClone(); 12 13 System.out.println(p); 14 System.out.println("============="); 15 System.out.println(p2); 16 System.out.println(p==p2);//fasle 17 18 } 19 20 }
1 package com.lovo.prototype.two; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.IOException; 6 import java.io.ObjectInputStream; 7 import java.io.ObjectOutputStream; 8 import java.io.Serializable; 9 import java.util.List; 10 11 12 13 public class Player implements Cloneable,Serializable { 14 private static final long serialVersionUID = 1L; 15 private int id; 16 private String name; 17 private String skill; 18 private List<String> list; 19 20 public List<String> getList() { 21 return list; 22 } 23 public void setList(List<String> list) { 24 this.list = list; 25 } 26 public int getId() { 27 return id; 28 } 29 public void setId(int id) { 30 this.id = id; 31 } 32 public String getName() { 33 return name; 34 } 35 public void setName(String name) { 36 this.name = name; 37 } 38 public String getSkill() { 39 return skill; 40 } 41 public void setSkill(String skill) { 42 this.skill = skill; 43 } 44 45 @Override 46 public String toString() { 47 return "Player [id=" + id + ", name=" + name + ", skill=" + skill 48 + ", list=" + list + "]"; 49 } 50 51 52 /** 53 * 深克隆 54 * @return 55 * @throws IOException 56 * @throws ClassNotFoundException 57 */ 58 public Player deepClone() throws IOException, ClassNotFoundException{ 59 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 60 ObjectOutputStream oos=new ObjectOutputStream(bos); 61 oos.writeObject(this); 62 63 ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray()); 64 ObjectInputStream ois=new ObjectInputStream(bis); 65 Player p=(Player) ois.readObject(); 66 67 68 69 70 return p; 71 72 } 73 74 75 }
1 package com.lovo.prototype.two; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 public class Test { 8 9 public static void main(String[] args) throws CloneNotSupportedException, ClassNotFoundException, IOException { 10 Player p=new Player(); 11 p.setId(1); 12 p.setName("小明明"); 13 p.setSkill("灌篮"); 14 15 Player p2=p.deepClone(); 16 List<String> list=new ArrayList<>(); 17 list.add("跑步"); 18 list.add("射击"); 19 p2.setList(list); 20 21 22 System.out.println(p); 23 System.out.println("============="); 24 System.out.println(p2); 25 26 } 27 28 }
原文:http://www.cnblogs.com/hellokitty1/p/4643989.html