Java clone simple learning

Source: Internet
Author: User

I often deal with these object classes when I am helping to write a single side recently, because I am not very familiar with java, and I just learned a little about it, I didn't go deep into learning, because I don't feel I can use it.

 

  • Protected Object clone () throws CloneNotSupportedException

Purpose: Create and return the copy of an object

When writing a single side, you sometimes need to create a new object from a known object. If you do not know it at the beginning, you can directly use:

  

Member memberA="Tom" GregorianCalendar(1998,7,10"596156210@qq.com"=memberA;

The intention is to change c without affecting A. But if this reference is made, it is obvious that c is changed because memberc and membera point to A memory address:

45586574558657

Later I asked the developer to know that we need to copy an object here. There are two ways to implement copy.

1. inherit the Cloneable Interface

Specific Practices:

  Member  .name=.birthday=.emailaddress=.gender= Object clone()  

Then, the cloned object points to a different memory address as membera. The specific test is as follows:

   main(String[] args) ="Tom" GregorianCalendar(1998,7,10"596156210@qq.com"==        memberc.setName("hello"//output

32512553
4558657
4558657
True
Hello
Tom

 

This is the first method. We can see that if the first method is clone, we must first inherit the external interface, then there will be exception detection, and we also need to Cast the cloned object.

The second method is implemented through the constructor.

    .name =.birthday=.emailaddress=.gender=

In this case, the new object is similar to the cloned object.

                Member membere=125907454558657

It is much more convenient. You don't have to inherit external interfaces, you don't have to worry about exceptions, or you don't need to use cast. I certainly use the second method when writing one side. After all, is it better to have less dependence on external systems?

Compared to python, copying is much easier.

 ===[1,4,3=[1, 3, 41, 4, 3 0.5s]

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.