JAVA 物件拷貝
JAVA 物件拷貝
為什麼需要有物件拷貝?
物件拷貝相對的自然是引用拷貝。java初學者經常會問,我這個方法要改變一個物件的屬性,可以把引數傳進去了,為什麼沒有改變了?
——基本資料型別傳值,而物件傳引用或引用的拷貝。
而有時候我們要獲取到一個當前狀態的物件複製品,他們是兩個獨立物件。不再是引用或者引用拷貝(實質都是指向物件本身)。就是說a是b的拷貝,b發生變化的時候,不要影響a。
物件拷貝有淺拷貝和深度拷貝兩種。
1)淺拷貝
淺拷貝是指物件中基本資料型別得到拷貝,而引用資料型別並未拷貝。
提到拷貝自然和clone聯絡起來了,所有具有clone功能的類都有一個特性,那就是它直接或間接地實現了Cloneable介面。
否則,我們在嘗試呼叫clone()方法時,將會觸發CloneNotSupportedException異常。
eg:
1
public
class
DOG
implements
Cloneable
2 {
3 public DOG(String name, int age)
4 {
5 this .name = name;
6 this .age = age;
7 }
8
9 public String getName()
10 {
11 return this .name;
12 }
13
14 public int getAge()
15 {
16 return this .age;
17 }
18
19 public Object clone()
20 {
21 try
22 {
23 return super .clone();
24
25 } catch (CloneNotSupportedException e)
26 {
27 return null ;
28 }
29 }
30
31 public String name;
32
33 private int age;
34
35 // test
36 public static void main(String[] args)
37 {
38 DOG dog1 = new DOG( " xiaogou " , 2 );
39 DOG dog2 = (DOG) dog1.clone();
40 dog1.name = " dagou " ;
41 System.out.println(dog2.getName());
42 System.out.println(dog2.getAge());
43 System.out.println(dog1.getName());
44 System.out.println(dog1.getAge());
45
46 }
47
48 }
49
2 {
3 public DOG(String name, int age)
4 {
5 this .name = name;
6 this .age = age;
7 }
8
9 public String getName()
10 {
11 return this .name;
12 }
13
14 public int getAge()
15 {
16 return this .age;
17 }
18
19 public Object clone()
20 {
21 try
22 {
23 return super .clone();
24
25 } catch (CloneNotSupportedException e)
26 {
27 return null ;
28 }
29 }
30
31 public String name;
32
33 private int age;
34
35 // test
36 public static void main(String[] args)
37 {
38 DOG dog1 = new DOG( " xiaogou " , 2 );
39 DOG dog2 = (DOG) dog1.clone();
40 dog1.name = " dagou " ;
41 System.out.println(dog2.getName());
42 System.out.println(dog2.getAge());
43 System.out.println(dog1.getName());
44 System.out.println(dog1.getAge());
45
46 }
47
48 }
49
執行結果:
xiaogou
2
dagou
2
2)深度拷貝
相對淺拷貝。實現物件中基本資料型別和引用資料型別的拷貝。
請先看下面程式碼:
1
class
AAA
2 {
3 public AAA(String name)
4 {
5 this .name = name;
6 }
7
8 public String name;
9 }
10
11 class DOG implements Cloneable
12 {
13 public DOG(String name, int age, AAA birthday)
14 {
15 this .name = name;
16 this .age = age;
17 this .birthday = birthday;
18 }
19
20 public String getName()
21 {
22 return name;
23 }
24
25 public int getAge()
26 {
27 return age;
28 }
29
30 public AAA getBirthday()
31 {
32 return birthday;
33 }
34
35 public String getBirth(AAA a)
36 {
37 return a.name;
38 }
39
40 public String name;
41
42 private int age;
43
44 public AAA birthday;
45
46 public Object clone()
47 {
48 try
49 {
50 super .clone();
51 return super .clone();
52 } catch (Exception e)
53 {
54 return null ;
55 }
56 }
57 }
58
59 public class TestClone
60 {
61 public static void main(String[] args)
62 {
63 AAA Day = new AAA( " test " );
64 DOG dog1 = new DOG( " xiaogou " , 2 , Day);
65 DOG dog2 = (DOG) dog1.clone();
66 // dog2.birthday = (AAA) dog1.birthday.clone();
67 dog1.birthday.name = " 333 " ;
68 System.out.println(dog1.getBirth(dog1.birthday));
69 System.out.println(dog2.getBirth(dog2.birthday));
70 }
71 }
72
2 {
3 public AAA(String name)
4 {
5 this .name = name;
6 }
7
8 public String name;
9 }
10
11 class DOG implements Cloneable
12 {
13 public DOG(String name, int age, AAA birthday)
14 {
15 this .name = name;
16 this .age = age;
17 this .birthday = birthday;
18 }
19
20 public String getName()
21 {
22 return name;
23 }
24
25 public int getAge()
26 {
27 return age;
28 }
29
30 public AAA getBirthday()
31 {
32 return birthday;
33 }
34
35 public String getBirth(AAA a)
36 {
37 return a.name;
38 }
39
40 public String name;
41
42 private int age;
43
44 public AAA birthday;
45
46 public Object clone()
47 {
48 try
49 {
50 super .clone();
51 return super .clone();
52 } catch (Exception e)
53 {
54 return null ;
55 }
56 }
57 }
58
59 public class TestClone
60 {
61 public static void main(String[] args)
62 {
63 AAA Day = new AAA( " test " );
64 DOG dog1 = new DOG( " xiaogou " , 2 , Day);
65 DOG dog2 = (DOG) dog1.clone();
66 // dog2.birthday = (AAA) dog1.birthday.clone();
67 dog1.birthday.name = " 333 " ;
68 System.out.println(dog1.getBirth(dog1.birthday));
69 System.out.println(dog2.getBirth(dog2.birthday));
70 }
71 }
72
執行結果是:
333
333
而真正要實現拷貝還的加點程式碼,如下請對比上面和下面程式碼的異同之處:
1
class
AAA
implements
Cloneable
2 {
3 public AAA(String name)
4 {
5 this .name = name;
6 }
7
8 public Object clone()
9 {
10 try
11 {
12 super .clone();
13 return super .clone();
14 } catch (Exception e)
15 {
16 return null ;
17 }
18 }
19
20 public String name;
21 }
22
23 class DOG implements Cloneable
24 {
25 public DOG(String name, int age, AAA birthday)
26 {
27 this .name = name;
28 this .age = age;
29 this .birthday = birthday;
30 }
2 {
3 public AAA(String name)
4 {
5 this .name = name;
6 }
7
8 public Object clone()
9 {
10 try
11 {
12 super .clone();
13 return super .clone();
14 } catch (Exception e)
15 {
16 return null ;
17 }
18 }
19
20 public String name;
21 }
22
23 class DOG implements Cloneable
24 {
25 public DOG(String name, int age, AAA birthday)
26 {
27 this .name = name;
28 this .age = age;
29 this .birthday = birthday;
30 }
相關文章
- 物件深拷貝和淺拷貝物件
- 實現物件淺拷貝、深拷貝物件
- 聊聊物件深拷貝和淺拷貝物件
- jquery之物件拷貝深拷貝淺拷貝案例講解jQuery物件
- 物件拷貝方式物件
- PHP 物件導向 - 物件的淺拷貝與深拷貝PHP物件
- 【JavaScript】物件的淺拷貝與深拷貝JavaScript物件
- 如何對JAVA物件進行深拷貝Java物件
- Java深拷貝和淺拷貝Java
- JavaScript物件的深拷貝以及淺拷貝分析JavaScript物件
- vue物件的拷貝Vue物件
- 一文搞懂Java引用拷貝、淺拷貝、深拷貝Java
- 安卓/Java物件拷貝(淺/深拷貝、兩種序列化、Beans等工具)安卓Java物件Bean
- JavaScript中物件的拷貝JavaScript物件
- vue的深度拷貝物件Vue物件
- 三目運算、物件克隆、深拷貝和淺拷貝物件
- Python - 物件賦值、淺拷貝、深拷貝的區別Python物件賦值
- 【Java】3-淺拷貝/ 2-深拷貝Java
- Java 輕鬆理解深拷貝與淺拷貝Java
- Java:org.apache.commons.beanutils.BeanUtils拷貝物件屬性JavaApacheBean物件
- Java NIO - 零拷貝Java
- 淺談Java中的淺拷貝和深拷貝Java
- java深克隆(深拷貝)和淺克隆(淺拷貝)Java
- 操作字元、物件方法, 深淺拷貝字元物件
- 拋磚引玉:拷貝物件,CopyObject物件Object
- js物件實現深淺拷貝!!JS物件
- 淺拷貝&深拷貝
- js陣列物件複製拷貝不改變原來的值,深拷貝JS陣列物件
- python 指標拷貝,淺拷貝和深拷貝Python指標
- 淺拷貝與深拷貝
- 淺拷貝和深拷貝
- 深拷貝和淺拷貝
- javascript對深拷貝物件的研坑JavaScript物件
- 談談Objective-C的物件拷貝Object物件
- 深入深入再深入 js 深拷貝物件JS物件
- Linux 和 Java 的零拷貝LinuxJava
- C++拷貝建構函式(深拷貝,淺拷貝)C++函式
- Python淺拷貝與深拷貝Python
- JavaScript深拷貝和淺拷貝JavaScript