Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Deep copy and shallow copy of Java Cloneable Interface

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly talks about "Deep copy and shallow copy method of Java Cloneable interface". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the "deep copy and shallow copy method of Java Cloneable interface".

Cloneable interface source code

Cloneable interface:

The class that implements this interface-- the clone () method that can be inferred from java.lang.Object can be legally called-- to implement the class instance: a copy of the attribute to the attribute.

If a class does not implement the Cloneable interface, a CloneNotSupportedException exception is thrown when the clone () method is called.

In general, subclasses that implement the Cloneable interface should override the clone () method with public access (although the clone method in the java.Object class is of type protected)

It should be recognized that the Cloneable interface does not contain the clone () method, so if you only implements the Cloneable interface, you will not be able to clone objects properly.

[reason: even if the clone method is called reflexively, there is no guarantee that it will succeed]-- personal understanding is whether or not to override the Clone () method, or the existence of "shallow copy and deep copy" problems.

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) {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;//}} shallow copy case Pet class definition

Notice that the Pet class implements the Cloneable interface, but does not override the Clone () method (obviously: the Pet class does not have the ability to clone objects at this time).

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) {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;//}} Person class definition

Notice that the Person class implements the Cloneable interface and overrides the Clone () method. So, does the Person class have the ability to clone objects? Due to the problem of shallow copy, it is considered that this kind of object cloning ability is 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) {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;//}} shallow copy problem-Code testing

Why: at this time, the object cloning ability of the Person class is incomplete and defective? Because at this point, when the clone () method is called through the Person object, the clone of the value of its member property pet (the object of the Pet class) is only a simple copy of the heap memory address.

That is, to put it bluntly, the pet attribute values of Person objects and cloned objects share the same heap memory. The problem is obvious: when you set the pet property of a cloned object, it obviously affects the pet property value of the original Person object.

The code is demonstrated as follows:

/ / methods public static void main (String [] args) throws CloneNotSupportedException {testPerson ();} public static void testPerson () throws CloneNotSupportedException {Person p=new Person ("Zhang San", 14 new Pet ("Xiao Hei")); 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 ("Xiao Huang"); System.out.println (p); System.out.println (clone); System.out.println (p.equals (clone)); System.out.println (p.getPet () = = clone.getPet ());}

Deep copy case

So, how to achieve deep copy? The key lies in the above case, in a few lines of code that are commented out.

The Pet class overrides the clone () method

Call the clone method of Pet in the clone () method of Person

Shallow copy problem Resolution-Deep copy Code testing

The test code remains the same, run it again:

At this point, I believe you have a deeper understanding of the "deep copy and shallow copy method of Java Cloneable interface". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report