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

How to implement object copy in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces how to achieve object copy in Java. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Copy of reference 1 / / copy of reference 2 private static void copyReferenceObject () {3 Person p = new Person (23, "zhang"); 4 Person p1 = pten 5 System.out.println (p); 6 System.out.println (p1); 7}

The result printed here is:

Person@3654919e

Person@3654919e

As you can see, the result of the print is the same, that is, the reference to the two is the same object, and no new object is created. So to distinguish between a reference copy and an object copy, the following is the object copy.

Shallow copy

If there is a basic data type in pojo, except String, implement the Cloneable override Clone method. This is a shallow copy.

-code

1 package core.java.deeporshoawcopy; 2 3 / * * 4 * @ author DGW-PC 5 * @ date June 7, 2018 6 * @ see verification shallow copy generally requires entity classes to use wrapper types for references to other classes that exist in deep copy classes, and also needs to implement cloneable interface 7 * / 8 9 class Person implements Cloneable {10 private String name;11 private Integer age;12 public String getName () {13 return name 14} 15 public void setName (String name) {16 this.name = name;17} 18 public Integer getAge () {19 return age;20} 21 public void setAge (Integer age) {22 this.age = age;23} 24 @ Override25 protected Object clone () throws CloneNotSupportedException {26 / * Person pendant nulltrait 27 try {28 p = (Person) super.clone () 29} catch (CloneNotSupportedException e) {30} * / 31 return super.clone (); 32} 33} 34 35 public class Base {36 37 public static void main (String [] args) throws CloneNotSupportedException {38 Person person = new Person (); 39 person.setName ("a"); 40 person.setAge (12); 41 Person per1= (Person) person.clone () 42 per1.setName ("b"); 43 per1.setAge (14) 44 System.out.println (person.getName () + "> Deep copy-if your POJO does not have a basic data type, you can define the type yourself You can also use the Data provided by java as an example to see the following code to implement the clone method. When it comes to using copies, you must pay attention to whether there is a problem with the classes provided by other packages. 1 / * 2 * Return a copy of this object. 3 * / 4 public Object clone () {5 Date d = null; 6 try {7 d = (Date) super.clone (); 8 if (cdate! = null) {9 d.cdate = (BaseCalendar.Date) cdate.clone (); 10} 11} catch (CloneNotSupportedException e) {} / / Won't happen12 return d 13}-the basic deep copy code is short: you must wrap how many other reference types of other classes are wrapped by the main class, then everything else must implement the Cloneable interface and the clone method 1 package core.java.deeporshoawcopy 2 3 4 / * * 5 * @ author DGW-PC 6 * @ date 7 * @ see 7 June 2018 serialization Deep copy 8 * / 9 10 class Dog implements Cloneable {11 private String name;12 13 public String getName () {14 return name;15} 16 17 public void setName (String name) {18 this.name = name 19} 20 @ Override21 protected Object clone () throws CloneNotSupportedException {22 return super.clone (); 23} 24} 25 class User implements Cloneable {26 private String name;27 private Dog dog;28 public String getName () {29 return name;30} 31 public void setName (String name) {32 this.name = name;33} 34 public Dog getDog () {35 return dog 36} 37 public void setDog (Dog dog) {38 this.dog = dog;39} 40 @ Override41 protected Object clone () throws CloneNotSupportedException {42 User u = (User) super.clone (); 43 u.dog = (Dog) dog.clone (); / / multiple relationships need to be clarified in all 44 return u 45} 46} 47 48 public class ObjCloner {49 public static void main (String [] args) throws CloneNotSupportedException {50 Dog dog = new Dog (); 51 dog.setName ("Pastoral Dog"); 52 User user = new User (); 53 user.setDog (dog); 54 user.setName (Wang II); 55 User user1= (User) user.clone () 56 user1.setName (Zhang San); 57 Dog dog2 = new Dog (); 58 dog2.setName (German Shepherd); 59 user1.setDog (dog2); 60 System.out.println (user.getName () + "raised" + user.getDog (). GetName ()) 61 System.out.println (user1.getName () + "raised" + user1.getDog (). GetName ()); 62} 63} result: serialization to achieve deep copy-the following code focuses on the CloneObj method. 1 package core.java.deeporshoawcopy; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 import java.io.Serializable 8 9 / * 10 * @ author DGW-PC 11 * @ date June 7, 2018 12 * @ since serialization to achieve deep copy 13 * / 14 15 class Body implements Serializable {16 / * 17 * 18 * / 19 private static final long serialVersionUID = 1L; 20 private String name; 21 private Fonter fonter; 22 private Head head; 23 public String getName () {24 return name 25} 26 public void setName (String name) {27 this.name = name; 28} 29 public Fonter getFonter () {30 return fonter; 31} 32 public void setFonter (Fonter fonter) {33 this.fonter = fonter; 34} 35 public Head getHead () {36 return head; 37} 38 public void setHead (Head head) {39 this.head = head 40} 41 @ Override 42 public String toString () {43 return "Body [name=" > transferred from https://www.cnblogs.com/dgwblog/p/9152358.html so much about how to copy objects in Java. I hope the above content can help you and learn more. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report