cloneable接口的一段代码

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()));
	}

}

猜你喜欢

转载自738986.iteye.com/blog/1750808