Day 15 Objectapi作业

作业:完成对FirstLevel对象的深度克隆。

 1 class FirstLevel implements Cloneable {
 2     int firstIntValue;
 3     double firstDoubleValue;
 4     SecondLevel second;
 5 
 6     public FirstLevel(int firstIntValue, double firstDoubleValue, SecondLevel second) {
 7         this.firstIntValue = firstIntValue;
 8         this.firstDoubleValue = firstDoubleValue;
 9         this.second = second;
10     }
11 
12     @Override
13     protected Object clone() throws CloneNotSupportedException {
14         // 利用Object的Clone方法,
15         FirstLevel first = (FirstLevel) super.clone();
16 
17         // 修改 first对象中引用类型成员变量,比如 second 成员变量引用,指向被复制的那个FirstLevel对象的second对象的拷贝,
18 
19         return first;
20     }
21 
22     public void testProtected() throws CloneNotSupportedException {
23         // 跨包子类访问父类 ,访问FirstLevel自己从Object继承下来的Clone方法
24         clone(); // alt + enter
25 
26         FirstLevel firstLevel = new FirstLevel(10, 20);
27         firstLevel.clone();
28     }
29 }
30 
31 class SecondLevel implements Cloneable {
32     int secondIntValue;
33     double secondDoubleValue;
34     ThirdLevel third;
35 
36     public SecondLevel(int secondIntValue, double secondDoubleValue, ThirdLevel third) {
37         this.secondIntValue = secondIntValue;
38         this.secondDoubleValue = secondDoubleValue;
39         this.third = third;
40     }
41 
42     @Override
43     protected Object clone() throws CloneNotSupportedException {
44         SecondLevel clone = (SecondLevel) super.clone();
45 
46         // 修改SecondLevel这个对象中,引用类型的成员变量的值,让他指向,复制之后的成员变量所指向对象地址
47         return clone;
48     }
49 }
50 
51 class ThirdLevel implements Cloneable {
52     int thirdIntValue;
53     double thirdDouleValue;
54 
55     public ThirdLevel(int thirdIntValue, double thirdDouleValue) {
56         this.thirdIntValue = thirdIntValue;
57         this.thirdDouleValue = thirdDouleValue;
58     }
59 }

参考答案:

 

 

 

 

  1 package com.day015;
  2 
  3 //完成对FirstLevel对象的深度克隆。
  4 
  5 /*
  6  * 实现深度克隆的思路:
  7  * 1.首先每个类,知道自己有哪些成员变量,成员变量中哪些是引用类型成员变量
  8  * 2.所以让每个类自己实现对自己的深度克隆方法
  9  * 3.实现深度克隆就变得简单了:
 10  *         a.首先利用super.clone(),即object的clone方法,完成对自己的克隆
 11  *         b.如果,本类中,有引用类型的成员,那么因为所有类都是实现了对自己的深度克隆
 12  *           所以,直接调用引用类型成员所指向对象的clone方法,让他们自己完成自己的深度克隆
 13  *         c.将引用类型成员变量的值,让他们指向深度克隆后的对象
 14  * 
 15  * 这个思想有些类似于递归思想
 16  * 
 17  */
 18 public class Demo1 {
 19 
 20     public static void main(String[] args) throws CloneNotSupportedException {
 21         //测试object拷贝
 22         ThirdLevel thirdLevel = new ThirdLevel(30,30.3);
 23         SecondLevel secondLevel = new SecondLevel(20, 20.2, thirdLevel);
 24         FirstLevel firstLevel = new FirstLevel(10, 10.1, secondLevel);
 25 
 26 
 27         System.out.println("原对象:" + firstLevel);
 28         //利用clone方法进行复制
 29         FirstLevel firstClone = (FirstLevel) firstLevel.clone();
 30         System.out.println("克隆对象:" + firstClone);
 31         
 32         //修改原始对象
 33         secondLevel.secondIntValue = 22222;
 34         thirdLevel.thirdIntValue = 333;
 35 
 36         System.out.println("修改后的原对象:" + firstLevel);
 37         System.out.println("修改后的复制对象:" + firstClone);
 38 
 39     }
 40 
 41 }
 42 
 43 class FirstLevel implements Cloneable {
 44     int firstIntValue;
 45     double firstDoubleValue;
 46     SecondLevel second;
 47 
 48     public FirstLevel(int firstIntValue, double firstDoubleValue, SecondLevel second) {
 49         this.firstIntValue = firstIntValue;
 50         this.firstDoubleValue = firstDoubleValue;
 51         this.second = second;
 52     }
 53 
 54     @Override
 55     protected Object clone() throws CloneNotSupportedException {
 56         // 利用Object的Clone方法//复制FirstLevel对象本身
 57         FirstLevel clone = (FirstLevel) super.clone();
 58 
 59         // 修改 first对象中引用类型成员变量,比如 second 成员变量引用,指向被复制的那个FirstLevel对象的second对象的拷贝,
 60         clone.second = (SecondLevel) second.clone();
 61         return clone;
 62     }
 63 
 64     @Override
 65     public String toString() {
 66         return "FirstLevel [firstIntValue=" + firstIntValue + ", firstDoubleValue=" + firstDoubleValue + ", second="
 67                 + second + "]";
 68     }
 69     
 70 }
 71 
 72 class SecondLevel implements Cloneable {
 73     int secondIntValue;
 74     double secondDoubleValue;
 75     ThirdLevel third;
 76 
 77     public SecondLevel(int secondIntValue, double secondDoubleValue, ThirdLevel third) {
 78         this.secondIntValue = secondIntValue;
 79         this.secondDoubleValue = secondDoubleValue;
 80         this.third = third;
 81     }
 82 
 83     @Override
 84     protected Object clone() throws CloneNotSupportedException {
 85         SecondLevel clone = (SecondLevel) super.clone();
 86 
 87         // 修改SecondLevel这个对象中,引用类型的成员变量的值,让他指向,复制之后的成员变量所指向对象地址
 88 //        ThirdLevel thirdLevel = (ThirdLevel) third.clone();
 89 //        clone.third = thirdLevel;
 90         clone.third = (ThirdLevel) third.clone();
 91         return clone;
 92     }
 93 
 94     @Override
 95     public String toString() {
 96         return "SecondLevel [secondIntValue=" + secondIntValue + ", secondDoubleValue=" + secondDoubleValue + ", third="
 97                 + third + "]";
 98     }
 99     
100 }
101 
102 class ThirdLevel implements Cloneable {
103     int thirdIntValue;
104     double thirdDouleValue;
105 
106     public ThirdLevel(int thirdIntValue, double thirdDouleValue) {
107         this.thirdIntValue = thirdIntValue;
108         this.thirdDouleValue = thirdDouleValue;
109     }
110     
111     @Override
112     protected Object clone() throws CloneNotSupportedException {
113         return super.clone();
114     }
115 
116     @Override
117     public String toString() {
118         return "ThirdLevel [thirdIntValue=" + thirdIntValue + ", thirdDouleValue=" + thirdDouleValue + "]";
119     }
120     
121 }
原文地址:https://www.cnblogs.com/dust2017/p/12791519.html