您的位置:首页 > 编程语言

cloneable接口的一段代码

2013-01-05 20:55 190 查看
基本数据类型包括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 CloneNotSupportedException{
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
CloneNotSupportedException {
// 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()));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: