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]