62,615
社区成员
发帖
与我相关
我的任务
分享
public class Test implements Cloneable {
T t = new T();
public static void main(String[] args) throws CloneNotSupportedException {
Test[] test1 = { new Test()};
Test[] test2 = test1.clone();
System.out.println(test1[0].t == test2[0].t);// 问题
}
}
class T implements Cloneable {
}
有没有办法让test2[0].t是一个全新的克隆出来的对象?
public class Test implements Cloneable {
T t = new T();
public static void main(String[] args) throws CloneNotSupportedException {
Test[] test1 = { new Test()};
Test[] test2 = {(Test)test1[0].clone()};
System.out.println(test1[0].t == test2[0].t);//
}
public Test clone() throws CloneNotSupportedException {
Test test = (Test)super.clone();
test.t = (T)t.clone();
return test;
}
}
class T implements Cloneable {
public T clone() throws CloneNotSupportedException {
return (T)super.clone();
}
}
public class Test implements Cloneable {
T t = new T(1);
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
Test tc=(TestClone)super.clone();
tc.t=new T(2);
return tc;
}
public static void main(String[] args) throws CloneNotSupportedException {
Test[] test1 = { new Test()};
Test[] test2 = test1.clone();
System.out.println((test1[0].t).equals(test2[0].t));// 问题
System.out.println(test1[0].t == test2[0].t);// 问题
}
}
class T {
int i;
public T(int i) {
this.i=i;
}
}