1. Fake clone: the VM only clones the object's memory address. Modifying the cloned object content will affect the original object.
Public class Employee {public Employee () {} public Employee (String name, int age) {this. age = age; this. name = name ;}@ Override public String toString () {return "name:" + name + "age:" + age;} public String getName () {return name ;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public static void main (String [] args) {Employee demo1 = new Employee ("rollen", 20); System. out. println (demo1); Employee demo2 = demo1; demo2.setAge (100); demo2.setName ("hello world"); System. out. println (demo1); System. out. println (demo2);} private String name; private int age ;}Running result:
[Running result]
Name: rollen age: 20
Name: hello world age: 100
Name: hello world age: 100
========================================================== ========================================================== ================
2. the clone method is required for cloning objects that contain basic data types. Modifying the object attributes After cloning does not affect the attributes of the original object.
Class Address {public Address () {} public Address (String state, int number) {this. number = number; this. state = state;} @ Override public String toString () {return "state:" + state + "munber:" + number;} public String getState () {return state ;} public void setState (String state) {this. state = state;} public int getNumber () {return number;} public void setNumber (int number) {this. number = number;} private String state; private int number;} public class Employee implements Cloneable {public Employee () {} public Employee (String name, int age, Address address) {this. address = address; this. age = age; this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Address getAddress () {return address;} public void setAddress (Address address Address) {this. address = address ;}@ Override public String toString () {StringBuilder sb = new StringBuilder (); sb. append ("name:" + name + ","); sb. append ("age:" + age + "\ n"); sb. append ("Address:" + address); return sb. toString () ;}@ Override protected Employee clone () {Employee employee = null; try {employee = (Employee) super. clone ();} catch (CloneNotSupportedException e) {e. printStackTrace ();} return employee;} public static void main (String [] args) {System. out. println ("before cloning:"); Address add1 = new Address ("China", 1); Employee emp1 = new Employee ("rollen", 20, add1); System. out. println (emp1); System. out. println ("cloned"); Employee emp2 = emp1.clone (); emp2.setName ("hello world"); emp2.setAge (100); emp2.address. setNumber (2); emp2.address. setState ("USA"); System. out. println (emp1); System. out. println ("-----"); System. out. println (emp2);} private String name; private int age; private Address address ;}
[Running result ]:
Before cloning:
Name: rollen, age: 20
Address: state: China munber: 1
After cloning
Name: rollen, age: 20
Address: state: US munber: 2
-----
Name: hello world, age: 100
Address: state: US munber: 2
========================================================== ========================================================== ====================
3. Cloning of classes containing the replication Data Structure class also requires the clone method for complex data types in the class.
Class Address implements Cloneable {public Address () {} public Address (String state, int number) {this. number = number; this. state = state ;}@ Override public String toString () {return "state:" + state + "munber:" + number ;}@ Override protected Address clone () throws CloneNotSupportedException {Address address = null; address = (Address) super. clone (); return address;} public String getState () {return state;} public void setState (String state) {this. state = state;} public int getNumber () {return number;} public void setNumber (int number) {this. number = number;} private String state; private int number;} public class Employee implements Cloneable {public Employee () {} public Employee (String name, int age, Address address) {this. address = address; this. age = age; this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Address getAddress () {return address;} public void setAddress (Address address Address) {this. address = address ;}@ Override public String toString () {StringBuilder sb = new StringBuilder (); sb. append ("name:" + name + ","); sb. append ("age:" + age + "\ n"); sb. append ("Address:" + address); return sb. toString () ;}@ Override protected Employee clone () {Employee employee = null; try {employee = (Employee) super. clone (); employee. address = address. clone ();} catch (CloneNotSupportedException e) {e. printStackTrace ();} return employee;} public static void main (String [] args) {System. out. println ("before cloning:"); Address add1 = new Address ("China", 1); Employee emp1 = new Employee ("rollen", 20, add1); System. out. println (emp1); System. out. println ("cloned"); Employee emp2 = emp1.clone (); emp2.setName ("hello world"); emp2.setAge (100); emp2.setAddress (new Address ("USA ", 2); System. out. println (emp1); System. out. println ("-----"); System. out. println (emp2);} private String name; private int age; private Address address ;}
[Running result ]:
Before cloning:
Name: rollen, age: 20
Address: state: China munber: 1
After cloning
Name: rollen, age: 20
Address: state: China munber: 1
-----
Name: hello world, age: 100
Address: state: US munber: 2
========================================================== ========================================================== ==========
4. serialization interface and object cloning
Import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. objectInputStream; import java. io. objectOutputStream; import java. io. serializable; class Address implements Serializable {public Address () {} public Address (String state, int number) {this. number = number; this. state = state ;}@ Override public String toString () {StringBuilder sb = new StringBuilder (); sb. append ("number:" + number); sb. append ("state" + state + "\ n"); return sb. toString () ;}public String getState () {return state;} public void setState (String state) {this. state = state;} public int getNumber () {return number;} public void setNumber (int number) {this. number = number;} private String state; private int number;} public class Employee implements Cloneable, Serializable {public Employee () {} public Employee (String name, int age, Address address) {this. address = address; this. age = age; this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Address getAddress () {return address;} public void setAddress (Address address Address) {this. address = address ;}@ Override public String toString () {StringBuilder sb = new StringBuilder (); sb. append ("name:" + name + ","); sb. append ("age:" + age + "\ n"); sb. append ("Address:" + address); return sb. toString () ;}@ Override protected Employee clone () {Employee employee = null; ByteArrayOutputStream baos = new Employee (); try {ObjectOutputStream oos = new ObjectOutputStream (baos); oos. writeObject (this); oos. close (); ByteArrayInputStream bais = new ByteArrayInputStream (baos. toByteArray (); ObjectInputStream ois = new ObjectInputStream (bais); employee = (Employee) ois. readObject (); ois. close ();} catch (Exception e) {e. printStackTrace ();} return employee;} public static void main (String [] args) {System. out. println ("before cloning:"); Address add1 = new Address ("China", 1); Employee emp1 = new Employee ("rollen", 20, add1); System. out. println (emp1); System. out. println ("cloned"); Employee emp2 = emp1.clone (); emp2.setName ("hello world"); emp2.setAge (100); emp2.address. setNumber (2); emp2.address. setState ("USA"); System. out. println (emp1); System. out. println ("-----"); System. out. println (emp2);} private String name; private int age; private Address address ;}
Before cloning:
Name: rollen, age: 20
Address: number: 1state China
After cloning
Name: rollen, age: 20
Address: number: 1state China
-----
Name: hello world, age: 100
Address: number: 2state United States