楼主参考一下,1、在class sourceA中加入这个方法 public Object clone(){
try{
return super.clone();
}catch(CloneNotSupportedException e){
throw new InternalError();
}
}2、在用的时候:
classA a = (classA)b.clone();//b是一个之前定义好的classA的对象good luck
try{
return super.clone();
}catch(CloneNotSupportedException e){
throw new InternalError();
}
}2、在用的时候:
classA a = (classA)b.clone();//b是一个之前定义好的classA的对象good luck
拥有100多个属性的类是一个很糟糕的类----同意!!!
HashMap hmX = new HashMap();
HashMap hmY = new HashMap();public Object clone(){
A o = null;
try{
o = (A)super.clone();
}catch(CloneNotSupportedException e){
throw new InternalError();
}
o.hmX = (HashMap)o.hmX.clone();
o.hmY = (HashMap)o.hmY.clone();
return o;
}
}这个例子是thinking in java里的一个(稍做了变化)
因为HashMap支持clone方法,所以直接克隆就可以了。如果是自己写的对象,就要让那个对象也要支持克隆了。good luck