如果将clone()值设置为零,则原始对象值也会更改为零

发布于 2024-11-28 23:54:02 字数 1301 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪梦之恋 2024-12-05 23:54:02

不要将 bit 设置为与原始对象相同的 byte[],而是克隆它:

obj.bit = this.bit.clone();

此外,您不需要设置 num< /code>,因为它已经在 super.clone() 返回的对象上正确设置。

Don't set bit to the same byte[] as the original object, instead clone it as well:

obj.bit = this.bit.clone();

Also, you don't need to set num, because that will already be set correctly on the object returned by super.clone().

生生漫 2024-12-05 23:54:02
obj.setNum(this.num);
obj.setBit(this.bit);

这两行代码 (a) 是多余的,(b) 是错误的。如果您只想克隆中的原始字段值,则根本不需要这个,因为 super.clone() 已经为您完成了。如果您的目标是返回一个独立赋值的对象。由于 bit[] 是一个数组,即一个对象,因此您也应该克隆它。

obj.setNum(this.num);
obj.setBit(this.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.

扭转时空 2024-12-05 23:54:02

您没有克隆您的 bit 数组。

You are not cloning your bit array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文