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()));
}
}
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()));
}
}
相关文章推荐
- java之实现Cloneable接口的详解,克隆一个对象--对应有浅克隆和深克隆,概念结合代码深入理解
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- 如何实现Cloneable接口?
- 写的将skb copy/clone后转发到源地址的一段代码
- Cloneable接口和Object的clone()方法
- ICloneable接口 Clone 深拷贝 浅拷贝
- 自己写的一段解决ARRAYLIST批量接口调用以后乱序的代码
- 结合实例详解clone()函数,Cloneable接口以及深拷贝与浅拷贝的问题
- 结合实例详解clone()函数,Cloneable接口以及深拷贝与浅拷贝的问题
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Objcet.clone() 和Cloneable 接口
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- java.lang.Cloneable接口
- Cloneable 接口
- Java中Cloneable接口
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Cloneable,Comparable,Comparator接口
- Java 基础数据类型 和 深度克隆对象的2种方法(实现Cloneable接口或者实现对象序列化)