Cloneable interface:
The class that implements this interface--can be It is inferred that the clone() method ofjava.lang.Object
can be legally called - to implement class instance: property-to-property copying.
If a class does not implement the Cloneable interface, then when the clone() method is called, a CloneNotSupportedException exception will be thrown.
Generally,subclasses that implement the Cloneable interface should override the clone() methodwith public access permissions (although the clone method in the java.Object class is of protected type)
It should be realized that the Cloneable interface does not contain the clone() method. Therefore, if you just implement the Cloneable interface, you will not be able to clone the object normally.
[Reason: Even if it is called reflectively There is no guarantee that the cloning method will be successful] - My personal understanding is:It is caused by whether to override the Clone() method, or by the existence of the "shallow copy and deep copy" problem.
class Pet implements Cloneable{ //properties private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public Pet() { } public Pet(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name='" + name + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pet pet = (Pet) o; return Objects.equals(name, pet.name); } @Override public int hashCode() { return Objects.hash(name); } // @Override // public Pet clone() { // try { // return (Pet)super.clone(); // } catch (CloneNotSupportedException e) { // e.printStackTrace(); // } // return null; // } }
Note: The Pet class implements the Cloneable interface, but does not override the Clone() method (obviously: this The Pet class does not have the ability to clone objects).
class Pet implements Cloneable{ //properties private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public Pet() { } public Pet(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name='" + name + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pet pet = (Pet) o; return Objects.equals(name, pet.name); } @Override public int hashCode() { return Objects.hash(name); } // @Override // public Pet clone() { // try { // return (Pet)super.clone(); // } catch (CloneNotSupportedException e) { // e.printStackTrace(); // } // return null; // } }
Note: The Person class implements the Cloneable interface and also overrides the Clone() method. So, does the Person class have the ability to clone objects? (Due to the shallow copy problem, this object cloning capability is considered incomplete and defective).
class Pet implements Cloneable{ //properties private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public Pet() { } public Pet(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name='" + name + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pet pet = (Pet) o; return Objects.equals(name, pet.name); } @Override public int hashCode() { return Objects.hash(name); } // @Override // public Pet clone() { // try { // return (Pet)super.clone(); // } catch (CloneNotSupportedException e) { // e.printStackTrace(); // } // return null; // } }
Why is it said that the object cloning ability of the Person class is incomplete and defective at this time? Because at this time, when the clone() method is called through the Person object and the object is cloned, the clone of the value of its member attribute pet (an object of the Pet class) is just a simple copy of the memory address in the heap area.
That is: To put it bluntly, the pet attribute value of the Person object and the cloned object share the same heap area memory. The problem is obvious: when the pet attribute of the cloned object is set, it will obviously affect the pet attribute value of the original Person object.
The code demonstration is as follows:
//methods public static void main(String[] args) throws CloneNotSupportedException { testPerson(); } public static void testPerson() throws CloneNotSupportedException { Person p=new Person("张三",14,new Pet("小黑")); System.out.println(p); Person clone = (Person)p.clone(); System.out.println(clone); System.out.println(p.equals(clone)); System.out.println(p.getPet()==clone.getPet()); System.out.println("************"); clone.setAge(15); System.out.println(p); System.out.println(clone); System.out.println(p.equals(clone)); System.out.println("************"); clone.getPet().setName("小黄"); System.out.println(p); System.out.println(clone); System.out.println(p.equals(clone)); System.out.println(p.getPet()==clone.getPet()); }
So, how to implement deep copy? The key lies in the commented lines of code in the above case.
The test code remains unchanged, run again:
The above is the detailed content of Deep copy and shallow copy methods of Java Cloneable interface. For more information, please follow other related articles on the PHP Chinese website!