Java object replication method summary

Source: Internet
Author: User
Tags object serialization

Java object replication method summary

 

 
1. Using the constructor to copy an object is an address-based call. Directly Using "=" to connect does not achieve the purpose of copying an object. The following classes can copy objects through constructors. The limitation is that the domain of the class that requires the object to be generated is only the basic type, and there is no other reference type. If there is a reference type, the new object only copies of the reference type. They point to the same object, which is a light copy. Unless the reference type is also replicated to the constructor again, until all the copies target the basic type, which is a deep copy.

 

The first type: the domain only has the basic type.

Public class Employee {private String name; private int age;/*** constructor copying object */public Employee (Employee employee) {// TODO Auto-generated constructor stubname = employee. getName (); age = employee. getAge ();}/*** initialize */public Employee (String name, int age) {// TODO Auto-generated constructor stubthis. name = name; this. age = age ;}@ Overridepublic String toString () {// TODO Auto-generated method stubreturn name: + name +, age + age;} // omit set () and get () functions}

Class 2: Addres address, which has a reference type, must also be copied by the constructor.
/*** Creation Time: 2:38:13, January 1, September 8, 2014 * Project name: Test * @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 * class description: */public class Employee {private String name; private int age; private Address address Address;/*** initialization */public Employee (String name, int age, address Address) {// TODO Auto-generated constructor stubthis. name = name; this. age = age; this. address = new Address (address);}/*** constructor copy */public Employee (Employee) {// TODO Auto-generated constructor stubname = employee. getName (); age = employee. getAge (); address = employee. getAddress () ;}@ Overridepublic String toString () {// TODO Auto-generated method stubreturn name: + name +, age: + age +, address -- + address ;} // omit the set () and get () functions}/*** Creation Time: 2:52:18, January 1, September 8, 2014 Project name: test ** @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 class description: */public class Address {private String state; private String province; private String city; /*** copy the constructor */public Address (Address address) {// TODO Auto-generated constructor stubstate = Address. state; province = address. province; city = address. city;}/*** initialize */public Address (String state, String province, String city) {// TODO Auto-generated constructor stubthis. state = state; this. province = province; this. city = city ;}@ Overridepublic String toString () {// TODO Auto-generated method stubStringBuilder stringBuilder = new StringBuilder (); stringBuilder. append (Country: + state +,); stringBuilder. append (province: + province +,); stringBuilder. append (city: + city); return stringBuilder. toString ();} // omitting the set () and get () functions}
2. the clone effect of an object is highly consistent with that of copying an object using constructors. The restriction is that the domain of the class that generates the object is only of the basic type, and there is no other reference type. If there is a reference type, the new object only copies the reference type. They point to the same object and do not clone it. This is a shortest clone. Unless the reference type is cloned again, until all the clones target the basic type, which is the deep clone.
Note that all classes must implement the Cloneable interface and rewrite the clone () method.
The first type: the domain only has the basic type.
/
* ** Creation Time: 2:38:13, January 1, September 8, 2014 Project name: Test ** @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 class description: */public class Employee implements Cloneable {private String name; private int age;/*** initialization */public Employee (String name, int age) {// TODO Auto-generated constructor stubthis. name = name; this. age = age ;}@ Overrideprotected Employee clone () throws CloneNotSupportedException {// TODO Auto-generated method stubEmployee employee = null; employee = (Employee) super. clone (); return employee ;}@ Overridepublic String toString () {// TODO Auto-generated method stub // return name: + name +, age: + age +, address -- + address; return name: + name +, age: + age;} // omitted set () and get () functions}


Class 2: The Addres address of the reference type of the domain must also be cloned.
/*** Creation Time: September 8, 2014 2:38:13 Project name: Test ** @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 class description: */public class Employee implements Cloneable {private String name; private int age; private Address address Address;/***/public Employee (String name, int age, address Address) {// TODO Auto-generated constructor stubthis. name = name; this. age = age; this. address = address;} // @ Overrideprotected Employee clone () throws CloneNotSupportedException {// TODO Auto-generated method stubEmployee employee = null; employee = (Employee) super. clone (); employee. address = address. clone (); return employee ;}@ Overridepublic String toString () {// TODO Auto-generated method stub return name: + name +, age: + age +, address -- + address;} // omit set () and get () functions}/*** Creation Time: 2:52:18 pm, January 1, September 8, 2014 Project name: test ** @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 class description: */public class Address implements Cloneable {private String state; private String province; private String city; /*** initialize */public Address (String state, String province, String city) {// TODO Auto-generated constructor stubthis. state = state; this. province = province; this. city = city ;}@ Overrideprotected Address clone () throws CloneNotSupportedException {// TODO Auto-generated method stubAddress address = null; address = (Address) super. clone (); return address ;}@ Overridepublic String toString () {// TODO Auto-generated method stubStringBuilder stringBuilder = new StringBuilder (); stringBuilder. append (Country: + state +,); stringBuilder. append (province: + province +,); stringBuilder. append (city: + city); return stringBuilder. toString ();} // omitting the set () and get () functions}
3. Object serialization writes the object to be copied from the memory to the local file, then reads the object from the local file into the memory and assigns it to a new reference. Object serial number implements deep cloning.
Note: the object to be serialized must implement the Serializable interface. If there is a reference type in the domain, you must also implement the Serializable interface. Otherwise, a serialization error occurs.
/*** Creation Time: 2:36:34, January 1, September 8, 2014 * Project name: Test * @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 * class description: */public class ObjectCloneTest {/*** @ param args * @ throws CloneNotSupportedException */public static void main (String [] args) throws CloneNotSupportedException {// TODO Auto-generated method stubAddress address Address = new address (China, Beijing, Beijing); Employee employee = new Employee (CaO Yafeng, 23, address); System. out. println (employee); Employee employee2 = null; ObjectOutputStream out = null; ObjectInputStream in = null; try {out = new ObjectOutputStream (new FileOutputStream (employee. dat); out. writeObject (employee); in = new ObjectInputStream (new FileInputStream (employee. dat); employee2 = (Employee) in. readObject ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} System. out. println (employee2) ;}}/*** Creation Time: 2:38:13, January 1, September 8, 2014 Project name: Test ** @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 class description: */public class Employee implements Serializable {/*** serial number */private static final long serialVersionUID = 1L; private String name; private int age; private Address address Address; /***/public Employee (String name, int age, Address address Address) {// TODO Auto-generated constructor stubthis. name = name; this. age = age; this. address = address ;}@ Overridepublic String toString () {// TODO Auto-generated method stubreturn name: + name +, age: + age +, address -- + address ;} // omit the set () and get () functions}/*** Creation Time: 2:52:18, January 1, September 8, 2014 Project name: test ** @ author Cao Yanfeng * @ since JDK 1.6.0 _ 21 class description: */public class Address implements Serializable {/*** serial number */private static final long serialVersionUID = 1L; private String state; private String province; private String city;/*** initialization */public Address (String state, String province, String city) {// TODO Auto-generated constructor stubthis. state = state; this. province = province; this. city = city ;}@ Overridepublic String toString () {// TODO Auto-generated method stubStringBuilder stringBuilder = new StringBuilder (); stringBuilder. append (Country: + state +,); stringBuilder. append (province: + province +,); stringBuilder. append (city: + city); return stringBuilder. toString ();} // omitting the set () and get () functions}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.