Java clone simple learning

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Recently in the help when writing a unilateral, often dealing with the object class, because is not very familiar with the Java, just learning about, is a very shallow learning, and no in-depth study and ha, because it would not.


  Effect: creates and returns a copy object 

  When writing a unilateral, sometimes need to create a new object from a known object, do not know, is a direct use:

  

Member memberA=new Member(
                "Tom",
                new GregorianCalendar(1998,7,10),
                Sex.MAIL,
                ""
                );
        
Member memberc=memberA;

The intention is to get C, but not to A, but this quote, obviously, change C also changed, because memberc and membera points to a memory address.:

System.out.println(memberA.hashCode());
System.out.println(memberc.hashCode());
//output
4558657
4558657

And later asked to develop a know here need copy an object it, there are two ways to realize Copy

First: to inherit from the Cloneable interface

Specific practices:

public class Member implements Cloneable { 
    public enum Sex{
        MAIL,FEMAIL
    }
    
    private String name;
    private Calendar birthday;
    private String emailaddress;
    private Sex gender;
    
    public Member(String name,Calendar birthday,Sex gender,String emailaddress)
    {
        
        this.name=name;
        this.birthday=birthday;
        this.emailaddress=emailaddress;
        this.gender=gender;
    }
//Inheritance, and overriding the clone method can pull    
    @Override
    protected Object clone() throws CloneNotSupportedException {
            return super.clone();
    }
}

I then and membera point to a different memory address to pull through the object out of the clone, the specific test:

public static void main(String[] args) throws CloneNotSupportedException {
        // TODO Auto-generated method stub

        Member memberA=new Member(
                "Tom",
                new GregorianCalendar(1998,7,10),
                Sex.MAIL,
                ""
                );
        
        Member memberc=memberA;
        Member cloned=(Member)memberA.clone();
        
        //Clone, cloned and membera should point to a different memory address, but memberc and memberA are pointing to the same address.
        System.out.println(cloned.hashCode());
        System.out.println(memberA.hashCode());
        System.out.println(memberc.hashCode());
            
        //The same class, so it should be the same
        System.out.println(memberA.getClass().equals(cloned.getClass()));
                
        //One is clone, is a reference, so memberc and cloned name values should not be the same
        memberc.setName("hello");
        System.out.println(memberA.getName());
        System.out.println(cloned.getName());
}

//output

32512553

4558657

4558657

true

hello

Tom

 

This is the first approach, we can see the first approach clone, first of all to external interface, and then there is anomaly detection, but also to other Cast clone objects.

The second kind of way, to be passed to the constructor.

    public Member(Member member) {
          this.name = member.getName();
          this.birthday=member.getBirthday();
          this.emailaddress=member.getEmailaddress();
          this.gender=member.getGender();
          
       }

In this way, almost I new out and clone effect.

        //The copy constructor
        Member membere=new Member(memberA);
        System.out.println(membere.hashCode());
        System.out.println(memberA.hashCode());
//output
12590745
4558657

Feel so much more convenient, do not have to inherit the external interface, also don't tube abnormality, without cast, when writing a unilateral I must use second, is still dependent on the outside of the less.

 In contrast, python copy is simple.

if __name__=="__main__":
    import copy
    listA=[1,4,3]
    copyB=copy.copy(listA)
    copyB.sort()
    print copyB
    print listA
#output
[1, 3, 4]
[1, 4, 3]
[Finished in 0.5s]




 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Hellen at November 17, 2013 - 11:24 AM