java.lang.Object里面有一个方法clone(),看方法说明就知道是用于复制对象的可是方法确实protected(受保护)类型的.看得到却用不到.
其实clone()被定义为protected是有原因的,因为有些时候某些类实例我们不希望能被复制.那我们怎么用这个方法呢?只要使用一个super关键字就够了.下面例子可以解释清楚clone()的作用与其用法:
运行结果:
其实clone()被定义为protected是有原因的,因为有些时候某些类实例我们不希望能被复制.那我们怎么用这个方法呢?只要使用一个super关键字就够了.下面例子可以解释清楚clone()的作用与其用法:
1public class C
2{
3 static class Strings implements Cloneable
4 {
5 private String str;
6
7 public void SetStr(String s){
8 this.str = s;
9 }
10
11 public String GetStr(){
12 return this.str;
13 }
14
15 public Object clone()throws CloneNotSupportedException{
16 return super.clone();
17 }
18 }
19
20 public static void main(String[] args)
21 {
22 try{
23 Strings str1 = new Strings();
24 str1.SetStr("Hello World!");
25
26 System.out.println("-----------------");
27
28 System.out.println("str1.SetStr(\"Hello World!\");");
29 System.out.println("str2 = (Strings)str1.clone();");
30 Strings str2 = (Strings)str1.clone();
31 System.out.println("str1:"+str1.GetStr());
32 System.out.println("str2:"+str2.GetStr());
33 System.out.print("print object str1:");
34 System.out.println(str1);
35 System.out.print("print object str2:");
36 System.out.println(str2);
37
38 System.out.println("-----------------");
39
40 System.out.println("str2.setStr(\"Hello!\");");
41 str2.SetStr("Hello!");
42 System.out.println("str1:"+str1.GetStr());
43 System.out.println("str2:"+str2.GetStr());
44 System.out.print("print object str1:");
45 System.out.println(str1);
46 System.out.print("print object str2:");
47 System.out.println(str2);
48
49 System.out.println("-----------------");
50
51 System.out.println("str1 = str2;");
52 str1 = str2;
53 System.out.print("print object str1:");
54 System.out.println(str1);
55 System.out.print("print object str2:");
56 System.out.println(str2);
57
58 System.out.println("-----------------");
59 }catch(Exception ex){
60 System.out.println(ex.toString());
61 }
62 }
63}
2{
3 static class Strings implements Cloneable
4 {
5 private String str;
6
7 public void SetStr(String s){
8 this.str = s;
9 }
10
11 public String GetStr(){
12 return this.str;
13 }
14
15 public Object clone()throws CloneNotSupportedException{
16 return super.clone();
17 }
18 }
19
20 public static void main(String[] args)
21 {
22 try{
23 Strings str1 = new Strings();
24 str1.SetStr("Hello World!");
25
26 System.out.println("-----------------");
27
28 System.out.println("str1.SetStr(\"Hello World!\");");
29 System.out.println("str2 = (Strings)str1.clone();");
30 Strings str2 = (Strings)str1.clone();
31 System.out.println("str1:"+str1.GetStr());
32 System.out.println("str2:"+str2.GetStr());
33 System.out.print("print object str1:");
34 System.out.println(str1);
35 System.out.print("print object str2:");
36 System.out.println(str2);
37
38 System.out.println("-----------------");
39
40 System.out.println("str2.setStr(\"Hello!\");");
41 str2.SetStr("Hello!");
42 System.out.println("str1:"+str1.GetStr());
43 System.out.println("str2:"+str2.GetStr());
44 System.out.print("print object str1:");
45 System.out.println(str1);
46 System.out.print("print object str2:");
47 System.out.println(str2);
48
49 System.out.println("-----------------");
50
51 System.out.println("str1 = str2;");
52 str1 = str2;
53 System.out.print("print object str1:");
54 System.out.println(str1);
55 System.out.print("print object str2:");
56 System.out.println(str2);
57
58 System.out.println("-----------------");
59 }catch(Exception ex){
60 System.out.println(ex.toString());
61 }
62 }
63}
运行结果:
-----------------
str1.SetStr("Hello World!");
str2 = (Strings)str1.clone();
str1:Hello World!
str2:Hello World!
print object str1:C$Strings@de6ced
print object str2:C$Strings@c17164
-----------------
str2.setStr("Hello!");
str1:Hello World!
str2:Hello!
print object str1:C$Strings@de6ced
print object str2:C$Strings@c17164
-----------------
str1 = str2;
print object str1:C$Strings@c17164
print object str2:C$Strings@c17164
-----------------
由运行结果可以看出通过clone方法返回的对象是一个新对象,而且其属性与原对象完全一致,也就是原对象的副本.而通过"="赋值的话却得到的是同一个对象.str1.SetStr("Hello World!");
str2 = (Strings)str1.clone();
str1:Hello World!
str2:Hello World!
print object str1:C$Strings@de6ced
print object str2:C$Strings@c17164
-----------------
str2.setStr("Hello!");
str1:Hello World!
str2:Hello!
print object str1:C$Strings@de6ced
print object str2:C$Strings@c17164
-----------------
str1 = str2;
print object str1:C$Strings@c17164
print object str2:C$Strings@c17164
-----------------