本帖最后由 彭颖 于 2013-3-16 22:13 编辑
用法举个简单例子看一下怎么用- public class A implements Cloneable {
- public String name[];
-
- public A(){
- name=new String[2];
- }
- public Object clone() {
- A o = null;
- try {
- o = (A) super.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return o;
- }
- }
复制代码 -------------------------
然后测试- A a1=new A();
- A a2=new A();
- a1.name[0]="a";
- a1.name[1]="1";
- a2=(A)a1.clone();
- a2.name[0]="b";
- a2.name[1]="1";
- System.out.println("a1.name="+a1.name);
- System.out.println("a1.name="+a1.name[0]+a1.name[1]);
- System.out.println("a2.name="+a2.name);
- System.out.println("a2.name="+a2.name[0]+a2.name[1]);
复制代码 -----------------
结果:- a1.name=[Ljava.lang.String;@757aef
- a1.name=b1
- a2.name=[Ljava.lang.String;@757aef
- a2.name=b1
复制代码 ----------------
a1.name,a2.name的hash值都是@757aef,也就是说这种clone对name数组是clone他们的地址 |