DeepClone

Prerequisites:

芭比工廠要生廠100隻一模一樣的芭比與肯尼,二隻娃娃一起放在盒子中賣。請問如何寫?

Barbie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;

public class Barbie implements Serializable {
  public String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Ken ken;

  public Ken getKen() {
    return ken;
  }

  public void setKen(Ken ken) {
    this.ken = ken;
  }

  public Barbie deepclone() {
    // 寫到記憶體緩衝區
    ByteArrayOutputStream bos = null;
    // 物件裝飾串流
    ObjectOutputStream oos = null;
    // 讀取記憶體緩衝區
    ByteArrayInputStream bis = null;
    // 讀取物件
    ObjectInputStream ois = null;
    try {
      // 建立串流
      bos = new ByteArrayOutputStream();
      oos = new ObjectOutputStream(bos);
      // 物件寫到記憶體緩衝區
      oos.writeObject(this);
      // toByteArray() 將記憶體緩衝區複製到byte[]傳回
      // 建立讀取串流
      bis = new ByteArrayInputStream(bos.toByteArray());
      // 讀取物件
      ois = new ObjectInputStream(bis);
      // readObject()是Object物件
      // 向下轉型成Barbie才能使用Barbie的方法
      Barbie copy = (Barbie) ois.readObject();
      return copy;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      try {
        // 只需要關閉裝飾串流,詳見裝飾串流文章
        ois.close();
        oos.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

Ken

1
2
3
4
5
6
7
8
9
public class Ken implements Serializable {
  public String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

main主程式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Test {
  public static void main(String[] args) {
    Barbie barbie = new Barbie();
    barbie.setName("經典芭比");
    Ken ken = new Ken();
    ken.setName("Sugar Daddy 肯尼");
    barbie.setKen(ken);
    //複製3隻經典芭比
    Barbie barbie1 = barbie.deepclone();
    System.out.println(barbie1.getName() + "/" + barbie1.getKen().getName()
        + "/hashcode:" + barbie1.hashCode());

    Barbie barbie2 = barbie.deepclone();
    System.out.println(barbie2.getName() + "/" + barbie2.getKen().getName()
        + "/hashcode:" + barbie2.hashCode());

    Barbie barbie3 = barbie.deepclone();
    System.out.println(barbie3.getName() + "/" + barbie3.getKen().getName()
        + "/hashcode:" + barbie3.hashCode());
  }
}
經典芭比/Sugar Daddy 肯尼/hashcode:1673605040
經典芭比/Sugar Daddy 肯尼/hashcode:693632176
經典芭比/Sugar Daddy 肯尼/hashcode:326549596

results matching ""

    No results matching ""