locked
Clone object ? RRS feed

  • Question

  • Hello,

    How can clone all the properties from one object to another object?

    For example,

    class A {

       private String name;

        private int age;

       private boolean status;

    ...

    }

     

    class B : A {

       private String address;

    }

     

    A a = new A();

    (set all the properties for a)

    B b = new B();

    How can copy all the properties from a to b?

     

    Best regards,

    Eric

     

    Wednesday, January 3, 2007 1:45 AM

Answers

  •  

    class A
    {
      private  String     name;
      private  int           age;
      private  boolean  status;
     
      protected void SetProperties( A clone)
      {
        this.name  = String.Copy( clone.name );
        this.age     = clone.age;
        this.status = clone.status;
      }
      . . .
    }

    class B: A
    {
      private  String   address;

      public B ( A clone )
      {
        this.SetProperties( clone );
      }
      . . .
    }


    A a = new A( );
    // Set all properties for a
    B b = new B( a );
    // b's A-class properties are now set the same as a's A-class properties.


     

    Wednesday, January 3, 2007 2:26 AM
  • Hi, Eric

    As you want to have a deep copy of the common members from a base class to an inherited one, you may implement an interface or just a constructor for copying stuff in the inherited class like Axe wrote above.

    Further more, if a class wants instances of itself to be cloneable, the class should implement the ICloneable interface, which is defined as follows (only defines one method):


    public interface ICloneable {
    Object Clone();
    }

     
    The ICloneable interface doesn't explicitly state whether Clone should make a shallow copy of its fields or a deep copy. So you must decide for yourself what makes the most sense for your type and then clearly document what your Clone implementation does.

     

    Wednesday, January 3, 2007 2:27 AM

All replies

  •  

    class A
    {
      private  String     name;
      private  int           age;
      private  boolean  status;
     
      protected void SetProperties( A clone)
      {
        this.name  = String.Copy( clone.name );
        this.age     = clone.age;
        this.status = clone.status;
      }
      . . .
    }

    class B: A
    {
      private  String   address;

      public B ( A clone )
      {
        this.SetProperties( clone );
      }
      . . .
    }


    A a = new A( );
    // Set all properties for a
    B b = new B( a );
    // b's A-class properties are now set the same as a's A-class properties.


     

    Wednesday, January 3, 2007 2:26 AM
  • Hi, Eric

    As you want to have a deep copy of the common members from a base class to an inherited one, you may implement an interface or just a constructor for copying stuff in the inherited class like Axe wrote above.

    Further more, if a class wants instances of itself to be cloneable, the class should implement the ICloneable interface, which is defined as follows (only defines one method):


    public interface ICloneable {
    Object Clone();
    }

     
    The ICloneable interface doesn't explicitly state whether Clone should make a shallow copy of its fields or a deep copy. So you must decide for yourself what makes the most sense for your type and then clearly document what your Clone implementation does.

     

    Wednesday, January 3, 2007 2:27 AM