6.2.3 Object cloning
Cloneable interface, which indicates that a class provides a secure clone method. (A little understanding)
When creating a copy of a variable referenced by an object, both the original variable and the copy are referenced by the same object, and any change in the variable will affect another variable.
Employee original =new Employee("John Public",50000);
Employee copy=original;
copy.raiseSalary(10); //(a method of salary increase)
If you want copy to be a new object, its initial state is the same as original, but they will have different states later, in this case you can use the clone method.
Employee copy =original.clone();
copy.raiseSalary(10);(original unchanged)
For a class, you need to determine:
1) Whether the default clone method meets the requirements;
2) Whether clone can be called on a variable subobject to repair the default clone method;
3) Whether clone should not be used. (Default option)
If the first or second item is selected, the class must:
1) Implementation of Cloneable interface;
2) Redefine the clone method and specify the public access modifier.
Cloneable interface is one of the tagging interface s provided by java. Or it's called a marker interface. The general purpose of an interface is to ensure that a class implements one or a set of specific methods. The tag interface does not contain any methods, and its only function is to allow instanceof to be used in type queries.
Even if clone's default (shallow copy) implementation meets the requirements, it still needs to implement the Cloneable interface, redefining clone as public and calling super.clone (). Here is an example:
class Employee implements Cloneable { public Employee clone() throws CloneNotSupportedException { return (Employee) super.clone(); } }
To create a deep copy, (variable instance domains in cloned objects)
class Employee implements Cloneable { public Employee clone() throws CloneNotSupportedException { Employee cloned=(Employee) super.clone(); cloned.hireDay = (Date) hireDay.clone(); return cloned; } }
The following program clones an instance of the Employee class and then calls two changer methods. The raiseSalary method changes the value of the salary field, while the setHireDay method changes the state of the hireDay field. Neither of the two modifier methods affects the original object, because clone creates a deep copy.
1 package cc.openhome; 2 import java.util.Date; 3 import java.util.GregorianCalendar; 4 public class CloneTest { 5 public static void main(String[] args) { 6 try { 7 Employee original =new Employee("mama", 80000); 8 original.setHireDay(2018, 3, 19); 9 Employee copy=original.clone(); 10 copy.raiseSalary(10); 11 copy.setHireDay(2008, 8, 8); 12 System.out.println("original="+original); 13 System.out.println("copy="+copy); 14 } catch (CloneNotSupportedException e) { 15 e.printStackTrace(); 16 } 17 } 18 } 19 class Employee implements Cloneable 20 { 21 private String name; 22 private double salary; 23 private Date hireDay; 24 public Employee(String name,double salary) 25 { 26 this.name=name; 27 this.salary=salary; 28 hireDay=new Date(); 29 } 30 public Employee clone() throws CloneNotSupportedException 31 { 32 Employee cloned=(Employee) super.clone(); 33 cloned.hireDay = (Date) hireDay.clone(); 34 return cloned; 35 } 36 public void setHireDay(int year,int month,int day) 37 { 38 Date newHireDay =new GregorianCalendar(year, month-1, day).getTime(); 39 hireDay.setTime(newHireDay.getTime()); 40 } 41 public void raiseSalary(double byPercent) 42 { 43 double raise =salary*byPercent/100; 44 salary+=raise; 45 } 46 public String toString() 47 { 48 return "Employee[name="+name+",salary="+salary+",hireDay="+hireDay+"]"; 49 } 50 }

original=Employee[name=mama,salary=80000.0,hireDay=Mon Mar 19 00:00:00 CST 2018] copy=Employee[name=mama,salary=88000.0,hireDay=Fri Aug 08 00:00:00 CST 2008] Successful build (total time: 0 seconds)