[关闭]
@XQF 2017-02-15T09:44:48.000000Z 字数 2076 阅读 1327

如何复制对象?clone方法?什么是深复制?什么是浅复制?

java


情况的契机在于我们要保留一个当前对象的状态。要是直接使用则是简单的引用传递而已,改变热河一个引用指向的对象值,改变都会同步。

使用clone方法的步骤

1.浅复制

  1. 实现Cloneable标识接口
  2. 重写Object类的clone()方法
  3. 在clone方法中调用super.clone()
  4. 把浅复制的引用指向原型对象新的克隆体。
  1. class T implements Cloneable {
  2. private int a;
  3. public void setA(int a) {
  4. this.a = a;
  5. }
  6. public int getA() {
  7. return a;
  8. }
  9. public Object clone() {
  10. Object o = null;
  11. try {
  12. o = super.clone();
  13. } catch (CloneNotSupportedException e) {
  14. e.printStackTrace();
  15. }
  16. return o;
  17. }
  18. }
  19. public class Test {
  20. public static void main(String[] args) {
  21. T t1 = new T();
  22. t1.setA(1);
  23. T t2 = t1;
  24. System.out.println("简单引用修改前");
  25. System.out.println(t1.getA());
  26. System.out.println(t2.getA());
  27. System.out.println();
  28. System.out.println("简单引用修改后");
  29. t2.setA(2);
  30. System.out.println(t1.getA());
  31. System.out.println(t2.getA());
  32. T t3 = (T) t2.clone();
  33. System.out.println("clone后数值修改前");
  34. System.out.println(t2.getA());
  35. System.out.println(t3.getA());
  36. System.out.println();
  37. t3.setA(3);
  38. System.out.println("clone后数值修改前");
  39. System.out.println(t2.getA());
  40. System.out.println(t3.getA());
  41. }
  42. }

测试好像String类型也可以

  1. class T implements Cloneable {
  2. private String str;
  3. public String getString() {
  4. return str;
  5. }
  6. public void setString(String str) {
  7. this.str = str;
  8. }
  9. public Object clone() {
  10. T o = null;
  11. try {
  12. o = (T) super.clone();
  13. } catch (CloneNotSupportedException e) {
  14. e.printStackTrace();
  15. }
  16. return o;
  17. }
  18. }
  19. public class Test {
  20. public static void main(String[] args) {
  21. T t1 = new T();
  22. t1.setString("hehhe");
  23. T t2 = (T) t1.clone();
  24. t1.setString("XXXXX");
  25. System.out.println(t1.getString());
  26. System.out.println(t2.getString());
  27. }
  28. }

2.深复制

深复制与浅复制在代码上的区别是要是该对象内部含有非基本类型对象的话,还要把非基本类型对象复制一遍
o.attr=this.getAttr().clone()

  1. class T implements Cloneable {
  2. private Date d = new Date();
  3. public Date getDate() {
  4. return d;
  5. }
  6. public void setDate(Date d) {
  7. this.d = d;
  8. }
  9. public Object clone() {
  10. T o = null;
  11. try {
  12. o = (T) super.clone();
  13. } catch (CloneNotSupportedException e) {
  14. e.printStackTrace();
  15. }
  16. o.d = (Date) getDate().clone();
  17. return o;
  18. }
  19. }
  20. public class Test {
  21. public static void main(String[] args) {
  22. T t = new T();
  23. T t1 = (T) t.clone();
  24. T t2 = t1;
  25. System.out.println(t);
  26. System.out.println(t1);
  27. System.out.println(t2);
  28. }
  29. }

com.leetcode.xqf.T@131245a
com.leetcode.xqf.T@16f6e28
com.leetcode.xqf.T@16f6e28

image_1b8ug6nmm1of61vjk14jasfq1h119.png-17kB

查到这个Date类是实现了Cloneable接口的。

3.深复制与浅复制的区别

浅复制只是复制当前对象的引用对象
深复制全部复制个遍

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注