如果将clone()值设置为零,则原始对象值也会更改为零
我正在使用 clone() 方法复制我的对象。但是当副本发生修改时,原始对象也会被修改。我尝试在以下示例中复制我的问题。这里有 2 个类 ClassA 和 ClassB。
public class ClassB implements Cloneable
{
int num = 0;
byte[] bit = new byte[1];
//Getters and setters have been removed due to space constraint
public Object clone()
{
try
{
ClassB obj = (ClassB)super.clone();
obj.setNum(this.num);
obj.setBit(this.bit);
return obj;
} catch (CloneNotSupportedException e) {
return null; }
}
}
//这里是ClassA,其中包含main方法并使用clone
public class ClassA {
public void cloneMethod(){
ClassB objB = new ClassB();
objB.bit[0] = (byte)0x8;
objB.setNum(5);
ClassB objCopy = null;
objCopy = (ClassB) objB.clone();
if(objCopy.bit[0] != (byte)0x0)
{
objCopy.bit[0] = 0;
}
System.out.println(objB.bit[0]); //At this point the original object value is also modified.
}
public static void main(String args[])
{
ClassA a = new ClassA();
a.cloneMethod();
}
}
现在如何保留原始对象值?我知道克隆有某些缺点。我也尝试过使用新关键字,但没有帮助。请建议。 在我的原始代码中,我必须稍后使用原始对象位值进行更多计算,并且复制对象位值将为 0。 谢谢。
I am making a copy of my object by using clone() method. But when there is modification in the copy the original object is also modified. I have tried to replicate my issue in the following example. Here are 2 classes ClassA and ClassB.
public class ClassB implements Cloneable
{
int num = 0;
byte[] bit = new byte[1];
//Getters and setters have been removed due to space constraint
public Object clone()
{
try
{
ClassB obj = (ClassB)super.clone();
obj.setNum(this.num);
obj.setBit(this.bit);
return obj;
} catch (CloneNotSupportedException e) {
return null; }
}
}
//Here is ClassA whioch contains the main method and uses clone
public class ClassA {
public void cloneMethod(){
ClassB objB = new ClassB();
objB.bit[0] = (byte)0x8;
objB.setNum(5);
ClassB objCopy = null;
objCopy = (ClassB) objB.clone();
if(objCopy.bit[0] != (byte)0x0)
{
objCopy.bit[0] = 0;
}
System.out.println(objB.bit[0]); //At this point the original object value is also modified.
}
public static void main(String args[])
{
ClassA a = new ClassA();
a.cloneMethod();
}
}
Now how to retain the original object value? I know clone has certain disadvantages. I have tried with new key word also but it doesnt help.Please suggest.
in my original code I have to use the original object bit value later for some more calculation and the copy object bit value will be 0.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要将
bit
设置为与原始对象相同的byte[]
,而是克隆它:此外,您不需要设置
num< /code>,因为它已经在
super.clone()
返回的对象上正确设置。Don't set
bit
to the samebyte[]
as the original object, instead clone it as well:Also, you don't need to set
num
, because that will already be set correctly on the object returned bysuper.clone()
.这两行代码 (a) 是多余的,(b) 是错误的。如果您只想克隆中的原始字段值,则根本不需要这个,因为 super.clone() 已经为您完成了。如果您的目标是返回一个独立赋值的对象。由于 bit[] 是一个数组,即一个对象,因此您也应该克隆它。
These two lines of code are (a) redundant and (b) wrong. If you just want the original field values in the clone you don't need this at all, because super.clone() has already done it for you. If your aim is to return an independently-valued object. As bit[] is an array, i.e. an Object, you should clone it too.
您没有克隆您的
bit
数组。You are not cloning your
bit
array.