基本数据类型包括string可以用浅拷贝,数组用深拷贝
import java.util.Arrays;
public class CloneTest implements Cloneable{
public CloneTest(String ss) {
super();
this.ss = ss;
}
private int temp[];
public int[] getTemp() {
return temp;
}
public void setTemp(int[] temp) {
this.temp = temp;
}
private String ss;
public String getSs() {
return ss;
}
public void setSs(String ss) {
this.ss = ss;
}
public String toString(){
return this.ss;
}
public Object clone() throws CloneNotSupportedExcepti
on{
CloneTest c = (CloneTest) super.clone();//object对象转为clonetest对象
int t[] = Arrays.copyOf(this.getTemp(), this.getTemp().length);
c.setTemp(t);
return c;
}
public static void main(String[] args) throws CloneNotSupportedExcepti
on {
// TODO Auto-generated method stub
int[] ttt = {1,2,3};
CloneTest t = new CloneTest("aaaa");
t.setTemp(ttt);
CloneTest t2 = (CloneTest) t.clone();
//t2.setSs("bbbb");
Arrays.fill(ttt, 333);
System.out.println(Arrays.toString(t.getTemp()));
System.out.println(Arrays.toString(t2.getTemp()));
}
}