object assignment

前端 未结 7 1043
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 07:07

I have a scenario like:

MyClass obj1 = new MyClass();
............//some operations on obj1;
MyClass obj2 = new MyClass();
obj2 = obj1;

I h

相关标签:
7条回答
  • 2020-12-21 07:26

    The thing to remember with object assignment is the difference between variables and objects.

    In your example, obj1 and obj2 are variables. Variables can refer to objects, but they are not objects themselves.

    What your code does is, at the end, tell both obj1 and obj2 to refer to the same object.

    What you want to do is make a new object - which, as others pointed out, is most easily done through the ICloneable interface.

    0 讨论(0)
  • 2020-12-21 07:26

    Have an extension method on myClass : GetDeepCopy Manually get make a copy of the obj and return this in GetDeepCopy.

    So something like :

    myclass obj1 = new myclass();
    
    ...
    
    myclass obj2 = obj1.etDeepCopy();
    
    0 讨论(0)
  • 2020-12-21 07:28

    Make your MyClass implement ICloneable and use

    MyClass obj1 = new MyClass();
    ...
    MyClass obj2 = obj1.Clone();
    

    If MyClass is not clonable, you need to look up all characteristic values in obj1 and copy them to obj2, e.g.

    myclass obj2 = new myclass();
    obj2.color = obj1.color; // .Clone();
    obj2.size = obj1.size;
    obj2.numberOfLimbs = obj1.numberOfLimbs;
    // etc.
    
    0 讨论(0)
  • 2020-12-21 07:37

    To add to KennyTM's answer, object Clone() method makes a copy of the invoking object. There are two type of copies which can be made. Deep copy and shallow copy. In KennyTM's answer, a deep copy is made. In a deep copy, the original object and the copied object are completely independent of each other. For more info, read up on the docs for ICloneable.

    And the Clone() declaration could be something like this:

    public object Clone()
    { 
     Myclass obj=new Myclass();
     return obj;
    }
    
    0 讨论(0)
  • 2020-12-21 07:42

    Assuming that the types on the object are simple, could you simply write a function that performs a kind of MemberwiseClone e.g.

    MyClass obj = new MyClass();
    // do your thing
    MyClass objCopy = new MyClass();
    objCopy.IamInt = obj.IamInt;
    objCopy.IamString = obj.IamString;
    

    Also more generally I found this Jon Skeet article very helpful when considering referencing.

    0 讨论(0)
  • 2020-12-21 07:46

    If MyClass declares a copy constructor, you could do a

    MyClass obj2=new MyClass(obj1).
    

    Otherwise, you should create a function to copy as in:

    MyClass CopyMyClassObject(MyClass obj1)
    {
        MyClass Result = new MyClass();
        Result.Value1 = obj1.Value1;
        Result.Value2 = obj1.Value2;
        //...
        Result Valuen = obj1.Valuen;
        Result.Object1.Value1 = obj1.Object1.Value1;
        Result.Object1.Value2 = obj1.Object1.Value2;
        //...
        Result.Object1.Valuen = obj1.Object1.Valuen;
        //..and so on until all values have been assigned
        //The actual assignments will use whatever methods are provided in MyClass, of course.
        return Result;
    }
    

    After that, in your code you would simply do:

    MyClass obj2 = CopyMyClassObject(obj1);
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题