I. Java pseudo-Clone
In Java, for a basic type, you can clone with "=", but for reference types it is not easy to clone with "=", which is related to the memory usage space in Java, Java holds the base type and reference variable in the stack, and saves the object in the heap. For reference variables, using "=" modifies the reference instead of copying the objects in the heap, at which point the two reference objects are pointing to the same object, so if modifying one variable modifies the other object.
public class Employee { private String name; private int age;//omit get and set method @override public String toString () { return ' name: ' + name + ', Age: ' + Ages; }} Public class Test {public static void Main (string[] args) { System.out.println ("Before Cloning:"); Employee Employee1 = new Employee (); Employee1.setname ("Taro 1"); Employee1.setage (n); SYSTEM.OUT.PRINTLN ("Employee 1 Information:"); System.out.println (employee1); System.out.println ("After cloning:"); Employee employee2 = employee1; Employee2.setname ("Taro 2"); Employee2.setage (a); System.out.println ("Employee 2 Information:"); System.out.println (employee2); SYSTEM.OUT.PRINTLN ("Employee 1 Information:"); System.out.println (EMPLOYEE1); }}
Output:
Before cloning:
Employee 1 Information:
Name: Taro 1, Age: 12
After cloning:
Employee 2 Information:
Name: Taro 2, Age: 114
Employee 1 Information:
Name: Taro 2, Age: 114
As you can see, the employee1 and employ2 two reference variables point to an object at the same time, and when the domain of Employee2 is modified, the domain of EMPLOYEE11 is also modified and therefore a false clone.
Second, shallow cloning
Protect Object Clone ()
It is often necessary to overwrite the method and restrict access to public, a method that, for each domain in a class, contains only basic types and immutable reference types, such as String, or objects that cannot be changed during their lifetime, you can copy objects with shallow clones.
public class Address {private String state; Private String Province; private string city;//omit get and set @Override public String toString () {StringBuilder sb = new StringBuilder (); Sb.append ("Country:" + state + ","); Sb.append ("Province:" + Province + ","); Sb.append ("City:" + cities); return sb.tostring (); }}public class Employee implements cloneable {private String name; private int age; Private Address address;//omit get and Set@override public String toString () {StringBuilder sb = new StringBuilder (); Sb.append ("Name:" + name + ","); Sb.append ("Ages:" + age + "\ n"); Sb.append ("Addresses:" + address); return sb.tostring (); @Override public Employee Clone () {employee employee = NULL; try {employee = (employee) super.clone (); } catch (Clonenotsupportedexception e) {e.printstacktrace (); } return employee; }}public class Test {public static void Main (string[] args) {System.out.println ("before Cloning:"); Address address = new address ("China", "Jilin", "Changchun"); Employee Employee1 = new Employee ("Tomorrow Technology", "address"); SYSTEM.OUT.PRINTLN ("Employee 1 Information:"); System.out.println (EMPLOYEE1); System.out.println ("After cloning:"); Employee employee2 = Employee1.clone (); Employee2.getaddress (). SetState ("China"); Employee2.getaddress (). Setprovince ("Sichuan"); Employee2.getaddress (). Setcity ("Chengdu"); Employee2.setname ("Southwest Jiaotong University"); Employee2.setage (114); System.out.println ("Employee 2 Information:"); System.out.println (EMPLOYEE2); SYSTEM.OUT.PRINTLN ("Employee 1 Information:"); System.out.println (EMPLOYEE1); }}
Output:
Before cloning:
Employee 1 Information:
Name: Tomorrow technology, Age: 12
Address: Country: China, Province: Jilin, City: Changchun
After cloning:
Employee 2 Information:
Name: Southwest Jiaotong University, Age: 114
Address: Country: China, Province: Sichuan, City: Chengdu
Employee 1 Information:
Name: Tomorrow technology, Age: 12
Address: Country: China, Province: Sichuan, City: Chengdu
We find that the employee class also contains references to the Adress class adress, and we know that the Clone method defaults to shallow cloning, that is, the object referenced by the object is not cloned, but simply copying the reference. So in the example above, the Adress object has only one in memory, and Employee1 and Employee2 point to it, and any modification to it will affect the other object. So the value of the adress has also been modified.
Three, deep cloning
One is to add a clone method to the reference type. If you change the light clone code above to:
In the Adress class, add
Protected address Clone () { address address = null; try { address = (address) super.clone (); } catch (Clonenotsupportedexception e) { e.printstacktrace (); } return address; }
In Employee:
Public Employee Clone () { employee employee = NULL; try { employee = (employee) super.clone (); Employee.address = Address.clone (); } catch (Clonenotsupportedexception e) { e.printstacktrace (); }
Output:
Before cloning:
Employee 1 Information:
Name: Tomorrow technology, Age: 12
Address: Country: China, Province: Jilin, City: Changchun
After cloning:
Employee 2 Information:
Name: Southwest Jiaotong University, Age: 114
Address: Country: China, Province: Sichuan, City: Chengdu
Employee 1 Information:
Name: Tomorrow technology, Age: 12
Address: Country: China, Province: Jilin, City: Changchun
Deep cloning is achieved.
One way to do this is to override the Clone method and add a statement such as order.items= (LineItems) Items.clone (), which is to artificially add a copy of the referenced object. The disadvantage of this method is that if there are many references to the object, or if the reference set refers to a lot of heavy, then it is too troublesome. The industry's common approach is to use serialization and then crossdress the method to implement deep cloning. Because the object is written into the stream after serialization, all referenced objects are included, so crossdress is equal to a fully cloned object that is generated.
The requirement for this method is that the object (including the referenced object) must have the serializable interface in advance, otherwise it will be excluded from the copy process with the transient keyword.
Java Road _ Fake clone, Shallow clone, deep clone