1700461831
1700461832
代码清单13-10 浅拷贝
1700461833
1700461834
public class Thing implements Cloneable{
1700461835
1700461836
//定义一个私有变量
1700461837
1700461838
private ArrayList<String>arrayList=new ArrayList<String>();
1700461839
1700461840
@Override
1700461841
1700461842
public Thing clone(){
1700461843
1700461844
Thing thing=null;
1700461845
1700461846
try{
1700461847
1700461848
thing=(Thing)super.clone();
1700461849
1700461850
}catch(CloneNotSupportedException e){
1700461851
1700461852
e.printStackTrace();
1700461853
1700461854
}
1700461855
1700461856
return thing;
1700461857
1700461858
}
1700461859
1700461860
//设置HashMap的值
1700461861
1700461862
public void setValue(String value){
1700461863
1700461864
this.arrayList.add(value);
1700461865
1700461866
}
1700461867
1700461868
//取得arrayList的值
1700461869
1700461870
public ArrayList<String>getValue(){
1700461871
1700461872
return this.arrayList;
1700461873
1700461874
}
1700461875
1700461876
}
1700461877
1700461878
在Thing类中增加一个私有变量arrayLis,类型为ArrayList,然后通过setValue和getValue分别进行设置和取值,我们来看场景类是如何拷贝的,如代码清单13-11所示。
1700461879
1700461880
代码清单13-11 浅拷贝测试
[
上一页 ]
[ :1.700461831e+09 ]
[
下一页 ]