我正在尝试构建一种方法来深度复制一个类:
public MyClass clne() {
MyClass cpy = new MyClass();
cpy.var1 = getVar1(); //getVar1 is a method returning the value of var1
cpy.var2 = getVar2();
return cpy;
}
但是,当我运行时:
MyClass x = new MyClass();
x.var1 = 18;
MyClass y = x.clne();
y.myMethod(); // Where myMethod is a method changing the value of var1 to, say 4.
System.out.println(x.var1);
x.var1 的值为 4。
难道我做错了什么?谢谢 :)