一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java中的深拷貝(深復制)和淺拷貝(淺復制)介紹

Java中的深拷貝(深復制)和淺拷貝(淺復制)介紹

2019-12-14 14:48Microgoogle JAVA教程

這篇文章主要介紹了Java中的深拷貝(深復制)和淺拷貝(淺復制)介紹,需要的朋友可以參考下

深拷貝(深復制)和淺拷貝(淺復制)是兩個比較通用的概念,尤其在C++語言中,若不弄懂,則會在delete的時候出問題,但是我們在這幸好用的是Java。雖然java自動管理對象的回收,但對于深拷貝(深復制)和淺拷貝(淺復制),我們還是要給予足夠的重視,因為有時這兩個概念往往會給我們帶來不小的困惑。

淺拷貝是指拷貝對象時僅僅拷貝對象本身(包括對象中的基本變量),而不拷貝對象包含的引用指向的對象。深拷貝不僅拷貝對象本身,而且拷貝對象包含的引用指向的所有對象。舉例來說更加清楚:對象A1中包含對B1的引用,B1中包含對C1的引用。淺拷貝A1得到A2,A2 中依然包含對B1的引用,B1中依然包含對C1的引用。深拷貝則是對淺拷貝的遞歸,深拷貝A1得到A2,A2中包含對B2(B1的copy)的引用,B2 中包含對C2(C1的copy)的引用。

若不對clone()方法進行改寫,則調用此方法得到的對象即為淺拷貝,下面我們著重談一下深拷貝。

運行下面的程序,看一看淺拷貝:

?
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
class Professor0 implements Cloneable {
  String name;
  int age;
 
  Professor0(String name, int age) {
    this.name = name;
    this.age = age;
  }
 
  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
}
 
class Student0 implements Cloneable {
  String name;// 常量對象。
  int age;
  Professor0 p;// 學生1和學生2的引用值都是一樣的。
 
  Student0(String name, int age, Professor0 p) {
    this.name = name;
    this.age = age;
    this.p = p;
  }
 
  public Object clone() {
    Student0 o = null;
    try {
      o = (Student0) super.clone();
    } catch (CloneNotSupportedException e) {
      System.out.println(e.toString());
    }
 
    return o;
  }
}
 
public class ShallowCopy {
  public static void main(String[] args) {
    Professor0 p = new Professor0("wangwu", 50);
    Student0 s1 = new Student0("zhangsan", 18, p);
    Student0 s2 = (Student0) s1.clone();
    s2.p.name = "lisi";
    s2.p.age = 30;
    s2.name = "z";
    s2.age = 45;
    System.out.println("學生s1的姓名:" + s1.name + "\n學生s1教授的姓名:" + s1.p.name + "," + "\n學生s1教授的年紀" + s1.p.age);// 學生1的教授
  }
}

s2變了,但s1也變了,證明s1的p和s2的p指向的是同一個對象。這在我們有的實際需求中,卻不是這樣,因而我們需要深拷貝:

?
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
class Professor implements Cloneable {
  String name;
  int age;
 
  Professor(String name, int age) {
    this.name = name;
    this.age = age;
  }
 
  public Object clone() {
    Object o = null;
    try {
      o = super.clone();
    } catch (CloneNotSupportedException e) {
      System.out.println(e.toString());
    }
    return o;
  }
}
 
class Student implements Cloneable {
  String name;
  int age;
  Professor p;
 
  Student(String name, int age, Professor p) {
    this.name = name;
    this.age = age;
    this.p = p;
  }
 
  public Object clone() {
    Student o = null;
    try {
      o = (Student) super.clone();
    } catch (CloneNotSupportedException e) {
      System.out.println(e.toString());
    }
    o.p = (Professor) p.clone();
    return o;
  }
}
 
public class DeepCopy {
  public static void main(String args[]) {
    long t1 = System.currentTimeMillis();
    Professor p = new Professor("wangwu", 50);
    Student s1 = new Student("zhangsan", 18, p);
    Student s2 = (Student) s1.clone();
    s2.p.name = "lisi";
    s2.p.age = 30;
    System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age);// 學生1的教授不改變。
    long t2 = System.currentTimeMillis();
    System.out.println(t2-t1);
  }
}

當然我們還有一種深拷貝方法,就是將對象串行化:

?
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
62
63
64
import java.io.*;
//Serialization is time-consuming
class Professor2 implements Serializable {
  /**
   *
   */
  private static final long serialVersionUID = 1L;
  String name;
  int age;
 
  Professor2(String name, int age) {
    this.name = name;
    this.age = age;
  }
}
 
class Student2 implements Serializable {
  /**
   *
   */
  private static final long serialVersionUID = 1L;
  String name;// 常量對象。
  int age;
  Professor2 p;// 學生1和學生2的引用值都是一樣的。
 
  Student2(String name, int age, Professor2 p) {
    this.name = name;
    this.age = age;
    this.p = p;
  }
 
  public Object deepClone() throws IOException, OptionalDataException,
      ClassNotFoundException {
    // 將對象寫到流里
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(bo);
    oo.writeObject(this);
    // 從流里讀出來
    ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
    ObjectInputStream oi = new ObjectInputStream(bi);
    return (oi.readObject());
  }
 
}
 
public class DeepCopy2 {
 
  /**
   * @param args
   */
  public static void main(String[] args) throws OptionalDataException,
      IOException, ClassNotFoundException {
    long t1 = System.currentTimeMillis();
    Professor2 p = new Professor2("wangwu", 50);
    Student2 s1 = new Student2("zhangsan", 18, p);
    Student2 s2 = (Student2) s1.deepClone();
    s2.p.name = "lisi";
    s2.p.age = 30;
    System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age); // 學生1的教授不改變。
    long t2 = System.currentTimeMillis();
    System.out.println(t2-t1);
  }
 
}

但是串行化卻很耗時,在一些框架中,我們便可以感受到,它們往往將對象進行串行化后進行傳遞,耗時較多。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美a级在线 | 大陆国语自产精品视频在 | 无遮掩60分钟从头啪到尾 | 日本不卡在线视频高清免费 | 日本免费在线播放 | 亚洲欧洲日产国码 最新 | 32pao强力打造免费高速高清 | 欧美精品综合一区二区三区 | 久久99re2热在线播放7 | 91夜色视频 | 国产高清在线精品一区二区三区 | 波多野结衣同性系列698 | 青青久久久国产线免观 | 日韩大片在线播放 | 国产欧美一区二区三区久久 | 日本高清视频在线免费观看 | 国产馆 | 91精品久久国产青草 | ysl千人千色t9t9t9t9 | 五月天久久久 | 99任你躁精品视频 | 欧美日韩精品一区二区三区视频播放 | 久久国产热视频99rev6 | 激情图片 激情小说 | 亚洲AV久久久久久久无码 | 日本无翼乌漫画 | 天天操天天射天天色 | 欧美丰满大乳大屁在线观看股 | 免费高清特黄a 大片 | 精品九九视频 | 亚洲精品免费在线观看 | 午夜亚洲WWW湿好大 午夜想想爱 | 91庥豆果冻天美精东蜜桃传媒 | 精品精品国产自在香蕉网 | 天选之王漫画顾长歌免费阅读 | 欧美色图亚洲天堂 | 日本小视频网站 | free哆拍拍免费永久视频 | 91看片淫黄大片.在线天堂 | 韩国三级年轻小的胰子完整 | 日韩高清一区二区三区不卡 |