One, why use copy
new when new objects are created, is going to cost a great deal. In order to solve this problem java provides
Span style= "font-family: ' Consolas ';" >cloneable this excuse. The class that implements this excuse has the ability to be copied, and the copy is done in memory, rather than through the
We know that a copy has a depth, while a shallow copy has a problem with the incomplete properties of the copied object. So there is the following question.
two, shallow copy of the problem
1. Shallow copy, even if a known instance is used to assign a value to a new instance's member variable. When copying the base data type, the member variables of the two instance already have an empty
, and the assignment operation passes the value. When copying reference data, the assignment value passes the reference. This creates a problem: When you modify the
value of one of the object's reference data, the value of the other object's reference data is also modified. For example, the following example:
1 Public classGame {2 3 PrivateString game;4 5 PublicGame (String game) {6 This. Game =game;7 }8 9 PublicString Play () {Ten returngame; One } A - Public voidSetgame (String game) { - This. Game =game; the } -}
1 Public classPeopleImplementscloneable {2 3 PrivateString name;4 5 Privategame game;6 7 Publicpeople (String name, game game) {8 This. Name =name;9 This. Game =game;Ten } One A PublicString GetName () { - returnname; - } the - Public voidsetName (String name) { - This. Name =name; - } + - PublicGame Getgame () { + returngame; A } at - Public voidSetgame (game game) { - This. Game =game; - } - -@OverrideprotectedPeople Clone () { inPeople people =NULL; - Try { toPeople = (people)Super. Clone (); +}Catch(clonenotsupportedexception e) { - e.printstacktrace (); the } * returnpeople; $ }Panax Notoginseng}
1 Public voidTestclone () {2Game game =NewGame ("Assassin's Creed");3 4People people1 =NewPeople ("1", game);5People people2 =People1.clone ();6People2.setname ("2");7People people3 =People1.clone ();8People3.setname ("3");9 TenSystem.out.println ("Name:" +people1.getname () + "game:" +People1.getgame (). Play ()); OneSystem.out.println ("Name:" +people2.getname () + "game:" +People2.getgame (). Play ()); ASystem.out.println ("Name:" +people3.getname () + "game:" +People3.getgame (). Play ()); - -People3.getgame (). Setgame ("Ares")); the -System.out.println ("Name:" +people1.getname () + "game:" +People1.getgame (). Play ()); -System.out.println ("Name:" +people2.getname () + "game:" +People2.getgame (). Play ()); -System.out.println ("Name:" +people3.getname () + "game:" +People3.getgame (). Play ()); +}
1 Output: 2 3 Name:1game: Assassin's Creed 4 Name:2game: Assassin's Creed 5 Name:3game: Assassin's Creed 6 Name:1game: Ares 7 Name:2game: Ares 8 Name:3game: Ares
As can be seen from the above example, a shallow copy is really just a reference, and when the value of the referenced data is changed, the value of all references to that data is changed. The problem is that it appears on the Clone method.
Clone This method is called the Clone method of the parent class, Super.clone () is the Clone method of object. However, this clone method is flawed, and it does not copy all of the object's properties, but it has a selective copy.
The rules are as follows:
A. Base type: If the variable is a base type, copy its value. For example: int, float, and so on.
B. Object: If the variable is an object, copy its object reference, that is, the original object and the new object are common to the instance variable.
C.string string: If the variable is a string, copy its address reference. However, when modified, a new string is generated from the string pool.
Through the above rules, it can be seen that because People1, People2, People3 are actually pointing to the same reference, so modified one of the game, the other two have also been modified.
At this point, modifying the Clone method can solve this problem, such as:
1@OverrideprotectedPeople Clone () {2People people =NULL;3 Try {4People = (people)Super. Clone ();5People.setgame (NewGame (People.getgame (). Play ()));6}Catch(clonenotsupportedexception e) {7 e.printstacktrace ();8 }9 returnpeople;Ten}
But this can be problematic, and when the object is very large, it can be very troublesome. Then there is the following workaround.
Second, using serialization to achieve a copy of the object
Method: Write the original object into the byte stream and read it out of the byte stream, so that a new object can be created. And there is no reference sharing problem with the new object and the original object. Specific methods
As follows:
1 Public classCloneutil {2 3 Public Static<textendsSerializable>T Clone (T object) {4T cloneobj =NULL;5Bytearrayoutputstream BOS =NULL;6ObjectOutputStream Oos =NULL;7Bytearrayinputstream bis =NULL;8ObjectInputStream Ois =NULL;9 Try {TenBOS =NewBytearrayoutputstream (); OneOos =NewObjectOutputStream (BOS); A Oos.writeobject (object); - -bis =NewBytearrayinputstream (Bos.tobytearray ()); theOIS =NewObjectInputStream (bis); -Cloneobj =(T) ois.readobject (); -}Catch(Exception e) { - e.printstacktrace (); +}finally { - if(NULL!=ois) { + Try { A ois.close (); at}Catch(IOException e) { - e.printstacktrace (); - } - } - if(NULL!=bis) { - Try { in bis.close (); -}Catch(IOException e) { to e.printstacktrace (); + } - } the if(NULL!=Oos) { * Try { $ oos.close ();Panax Notoginseng}Catch(IOException e) { - e.printstacktrace (); the } + } A if(NULL!=Bos) { the Try { + bos.close (); -}Catch(IOException e) { $ e.printstacktrace (); $ } - } - } the returncloneobj; - }Wuyi}
Objects using the above method tool class need to implement the Serializable interface without implementing the Clone () method of the Cloneable interface.
Re-learn Java (4): Shallow copy deep copy