[关闭]
@x-power 2019-09-25T13:07:26.000000Z 字数 2791 阅读 649

深克隆和浅克隆

常用函数


浅克隆

浅克隆: 被复制对象的所有基本变量都含有和原来变量完全相同的值, 而其他所有的引用对象任然指向原来的对象. 换言之, 浅克隆仅仅复制锁考虑的对象, 而不复制它所引用的对象. 克隆的深度仅仅到栈内存中.

深克隆

深克隆: 和浅克隆不一样的点在于其重写了clone函数. 在克隆本身对象的时候, 也对其中的引用类型的属性进行克隆.



  1. package reBegin;
  2. public class CloneDemo{
  3. public static void main(String[] args) throws CloneNotSupportedException {
  4. /* 浅克隆 */
  5. WeakTestClone testClone = new WeakTestClone(1,"阿狗", new GG(77));
  6. WeakTestClone cloneWeak = (WeakTestClone) testClone.clone();
  7. // 这样会将所有克隆体和被克隆体的引用改变, 因为getGg() 得到了栈内存地址, setAge()的时候改变了堆内存的地址.
  8. // 因为两个对象的引用属性是指向同一个堆内存地址的,所以两个对象的gg 属性就改变了.
  9. testClone.getGg().setAge(110);
  10. testClone.setName("JackMa");
  11. System.out.println(testClone);
  12. System.out.println(cloneWeak);
  13. /* 深克隆 */
  14. StrongTestClone strongTestClone = new StrongTestClone(1,"阿狗",new GG(77));
  15. StrongTestClone cloneStrong = (StrongTestClone) strongTestClone.clone();
  16. // StrongTestClone 重写了clone函数, 在其中将其 gg 属性也一并克隆.
  17. // 这个时候对象中的gg属性, 存储的堆内存地址就不一样了. 是两个完全不同的对象.
  18. strongTestClone.getGg().setAge(111);
  19. strongTestClone.setName("JackMa");
  20. System.out.println(strongTestClone);
  21. System.out.println(cloneStrong);
  22. }
  23. }
  24. /**
  25. * 浅克隆, 只会克隆 仅仅拷贝对象本身, 也就是拷贝的深度仅限于栈内存.
  26. */
  27. class WeakTestClone implements Cloneable{
  28. private int age;
  29. private String name;
  30. private GG gg;
  31. WeakTestClone() {
  32. }
  33. WeakTestClone(int age, String name, GG gg) {
  34. this.age = age;
  35. this.name = name;
  36. this.gg = gg;
  37. }
  38. public int getAge() {
  39. return age;
  40. }
  41. public void setAge(int age) {
  42. this.age = age;
  43. }
  44. public String getName() {
  45. return name;
  46. }
  47. public void setName(String name) {
  48. this.name = name;
  49. }
  50. public GG getGg() {
  51. return gg;
  52. }
  53. public void setGg(GG gg) {
  54. this.gg = gg;
  55. }
  56. @Override
  57. protected Object clone() throws CloneNotSupportedException {
  58. return super.clone();
  59. }
  60. @Override
  61. public String toString() {
  62. return "testClone{" +
  63. "age=" + age +
  64. ", name='" + name + '\'' +
  65. ", gg=" + gg.getAge() +
  66. '}';
  67. }
  68. }
  69. class StrongTestClone implements Cloneable{
  70. private int age;
  71. private String name;
  72. private GG gg;
  73. public StrongTestClone() {
  74. }
  75. public StrongTestClone(int age, String name, GG gg) {
  76. this.age = age;
  77. this.name = name;
  78. this.gg = gg;
  79. }
  80. public int getAge() {
  81. return age;
  82. }
  83. public void setAge(int age) {
  84. this.age = age;
  85. }
  86. public String getName() {
  87. return name;
  88. }
  89. public void setName(String name) {
  90. this.name = name;
  91. }
  92. public GG getGg() {
  93. return gg;
  94. }
  95. public void setGg(GG gg) {
  96. this.gg = gg;
  97. }
  98. @Override
  99. public String toString() {
  100. return "StrongTestClone{" +
  101. "age=" + age +
  102. ", name='" + name + '\'' +
  103. ", gg=" + gg.getAge() +
  104. '}';
  105. }
  106. @Override
  107. protected Object clone() throws CloneNotSupportedException {
  108. StrongTestClone strongTestClone = (StrongTestClone) super.clone();
  109. strongTestClone.setGg((GG)gg.clone());
  110. return strongTestClone;
  111. }
  112. }
  113. class GG implements Cloneable{
  114. private int age;
  115. public GG() {
  116. }
  117. GG(int age) {
  118. this.age = age;
  119. }
  120. public int getAge() {
  121. return age;
  122. }
  123. public void setAge(int age) {
  124. this.age = age;
  125. }
  126. @Override
  127. protected Object clone() throws CloneNotSupportedException {
  128. return super.clone();
  129. }
  130. }
  1. <!-- 结果 -->
  2. testClone{age=1, name='JackMa', gg=110}
  3. testClone{age=1, name='阿狗', gg=110}
  4. StrongTestClone{age=1, name='JackMa', gg=111}
  5. StrongTestClone{age=1, name='阿狗', gg=77}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注