Java拷贝构造函数初尝试
Posted Chinda
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java拷贝构造函数初尝试相关的知识,希望对你有一定的参考价值。
浅复制(浅克隆) :被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
深复制(深克隆) :被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把要复制的对象所引用的对象都复制了一遍。
1 package com.itinfo; 2 3 /** 4 * 测试实体类 5 * 6 * @author Wáng Chéng Dá 7 * @create 2017-02-24 10:41 8 */ 9 public class PersonT { 10 11 private String name; 12 13 private int age; 14 15 private String sex; 16 17 public PersonT() { 18 } 19 20 public PersonT(String name, int age, String sex) { 21 this.name = name; 22 this.age = age; 23 this.sex = sex; 24 } 25 26 public PersonT(PersonT personT) { 27 this.name = personT.name; 28 this.age = personT.age; 29 this.sex = personT.sex; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public int getAge() { 41 return age; 42 } 43 44 public void setAge(int age) { 45 this.age = age; 46 } 47 48 public String getSex() { 49 return sex; 50 } 51 52 public void setSex(String sex) { 53 this.sex = sex; 54 } 55 56 @Override 57 public String toString() { 58 return "PersonT{" + 59 "name=\'" + name + \'\\\'\' + 60 ", age=" + age + 61 ", sex=\'" + sex + \'\\\'\' + 62 \'}\'; 63 } 64 }
深拷贝:
1 package com.itinfo; 2 3 /** 4 * 浅析浅拷贝和深拷贝 5 * 6 * @author Wáng Chéng Dá 7 * @create 2017-02-24 8:22 8 */ 9 public class Asian { 10 private String skin; 11 private PersonT person; 12 13 public Asian() { 14 } 15 16 public Asian(String skin, PersonT person) { 17 this.skin = skin; 18 this.person = person; 19 } 20 21 public Asian(Asian asian) { 22 this.skin = asian.skin; 23 this.person = new PersonT(asian.person); 24 } 25 26 // public Asian(Asian asian) { 27 // this(asian.skin, asian.person); 28 // } 29 30 public String getSkin() { 31 return skin; 32 } 33 34 public void setSkin(String skin) { 35 this.skin = skin; 36 } 37 38 public PersonT getPerson() { 39 return person; 40 } 41 42 public void setPerson(PersonT person) { 43 this.person = person; 44 } 45 46 @Override 47 public String toString() { 48 return "Asian{" + 49 "skin=\'" + skin + \'\\\'\' + 50 ", person=" + person.toString() + 51 \'}\'; 52 } 53 54 55 public static void main(String[] args) { 56 PersonT p1 = new PersonT("张三", 14, "男"); 57 PersonT p2 = new PersonT(p1); 58 System.out.println(p1.toString()); 59 System.out.println(p2.toString()); 60 61 //修改p1 62 System.out.println("---------修改p1----------"); 63 p1.setName("李四"); 64 p1.setAge(19); 65 p1.setSex("女"); 66 System.out.println(p1.toString()); 67 System.out.println(p2.toString()); 68 69 //修改p2 70 System.out.println("---------修改p2----------"); 71 p2.setName("王二"); 72 p2.setAge(23); 73 p2.setSex("男"); 74 System.out.println(p1.toString()); 75 System.out.println(p2.toString()); 76 77 78 System.out.println("-------------------------"); 79 Asian a1 = new Asian("yellow", p1); 80 Asian a2 = new Asian(a1); 81 System.out.println(a1.toString()); 82 System.out.println(a2.toString()); 83 84 //修改a1 85 System.out.println("---------修改a1----------"); 86 a1.setSkin("back"); 87 a1.getPerson().setName("麻子"); 88 a1.getPerson().setAge(28); 89 a1.getPerson().setSex("男"); 90 System.out.println(a1.toString()); 91 System.out.println(a2.toString()); 92 93 //修改a2 94 System.out.println("---------修改a2----------"); 95 a2.setSkin("red"); 96 a2.getPerson().setName("诗诗"); 97 a2.getPerson().setAge(25); 98 a2.getPerson().setSex("女"); 99 System.out.println(a1.toString()); 100 System.out.println(a2.toString()); 101 102 } 103 }
控制台输出:
PersonT{name=\'张三\', age=14, sex=\'男\'} |
内存分析:
浅拷贝:
1 package com.itinfo; 2 3 /** 4 * 浅析浅拷贝和深拷贝 5 * 6 * @author Wáng Chéng Dá 7 * @create 2017-02-24 8:22 8 */ 9 public class Asian { 10 private String skin; 11 private PersonT person; 12 13 public Asian() { 14 } 15 16 public Asian(String skin, PersonT person) { 17 this.skin = skin; 18 this.person = person; 19 } 20 21 // public Asian(Asian asian) { 22 // this.skin = asian.skin; 23 // this.person = new PersonT(asian.person); 24 // } 25 26 public Asian(Asian asian) { 27 this(asian.skin, asian.person); 28 } 29 30 public String getSkin() { 31 return skin; 32 } 33 34 public void setSkin(String skin) { 35 this.skin = skin; 36 } 37 38 public PersonT getPerson() { 39 return person; 40 } 41 42 public void setPerson(PersonT person) { 43 this.person = person; 44 } 45 46 @Override 47 public String toString() { 48 return "Asian{" + 49 "skin=\'" + skin + \'\\\'\' + 50 ", person=" + person.toString() + 51 \'}\'; 52 } 53 54 55 public static void main(String[] args) { 56 PersonT p1 = new PersonT("张三", 14, "男"); 57 PersonT p2 = new PersonT(p1); 58 System.out.println(p1.toString()); 59 System.out.println(p2.toString()); 60 61 //修改p1 62 System.out.println("---------修改p1----------"); 63 p1.setName("李四"); 64 p1.setAge(19); 65 p1.setSex("女"); 66 System.out.println(p1.toString()); 67 System.out.println(p2.toString()); 68 69 //修改p2 70 System.out.println("---------修改p2----------"); 71 p2.setName("王二"); 72 p2.setAge(23); 73 p2.setSex("男"); 74 System.out.println(p1.toString()); 75 System.out.println(p2.toString()); 76 77 78 System.out.println("-------------------------"); 79 Asian a1 = new Asian("yellow", p1); 80 Asian a2 = new Asian(a1); 81 System.out.println(a1.toString()); 82 System.out.println(a2.toString()); 83 84 //修改a1 85 System.out.println("---------修改a1----------"); 86 a1.setSkin("back"); 87 a1.getPerson().setName("麻子"); 88 a1.getPerson().setAge(28); 89 a1.getPerson().setSex("男"); 90 System.out.println(a1.toString()); 91 System.out.println(a2.toString()); 92 93 //修改a2 94 System.out.println("---------修改a2----------"); 95 a2.setSkin("red"); 96 a2.getPerson().setName("诗诗"); 97 a2.getPerson().setAge(25); 98 a2.getPerson().setSex("女"); 99 System.out.println(a1.toString()); 100 System.out.println(a2.toString()); 101 102 } 103 }
控制台输出:
PersonT{name=\'张三\', age=14, sex=\'男\'} |
内存分析:
以上是关于Java拷贝构造函数初尝试的主要内容,如果未能解决你的问题,请参考以下文章