Java Basic Learning--Cloning of objects

Source: Internet
Author: User

Objects are cloned into shallow clones and deep clones of objects

First, shallow cloning of objects

Precautions:

1. If an object needs to invoke clone's method clone, then the object must implement the Cloneable interface

The 2.Cloneable object is just an identity object, and there is no method

1  Public classDemo1 {2      Public Static voidMain (string[] args)throwsException {3Person person =NewPerson ("001", "Dog Doll");4Person Person2 =Person.clone ();5Person2.setname ("Abd");6 System.out.println (person.tostring ());7 System.out.println (person2.tostring ());8     }9}

Output results?

Item No: 001 Name: Dog Doll Number: 001 Name: Abd
View Code

3. Shallow cloning of objects, when cloning an object, if the cloned object maintains another class of objects, this time just to clone an object's address and not another object is cloned one copy.

, a shallow clone of an object simply clones the address of the addresses object, so that when we modify the P2 object, the contents of its memory address are modified, so the output city result is the same.

4. A shallow clone of the object is not called to the constructor method.

Second, deep cloning of objects

Deep cloning of objects: the deep cloning of objects, is the use of the object's input and output stream, the object is written to the file, and then read the object information, this process is called the object's deep cloning.

1  Public classDemo2 {2      Public Static voidMain (string[] args)throwsIOException, ClassNotFoundException {3Adress Adress =NewAdress ("Beijing"));4Person person =NewPerson ("002", "Sony Pig", adress);5 writobj (person);6Person Person2 =readobj (person);7person2.adress.city = "Shenzhen";8System.out.println ("Person1:" +person.tostring ());9System.out.println ("Person2:" +person2.tostring ());Ten     } One      Public StaticPerson Readobj (person person)throwsIOException, classnotfoundexception{ AFileInputStream FileInputStream =NewFileInputStream ("E:\\obj.txt"); -ObjectInputStream ObjectInputStream =NewObjectInputStream (fileinputstream); -         return(person) objectinputstream.readobject (); the     } -      Public Static voidWritobj (person person)throwsioexception{ -         //set the output stream of the file to the image -FileOutputStream FileOutputStream =NewFileOutputStream ("E:\\obj.txt"); +ObjectOutputStream ObjectOutputStream =NewObjectOutputStream (fileoutputstream); - Objectoutputstream.writeobject (person); + objectoutputstream.close (); A     } at}

Output results?

Person1: No.: 002 Name: Sony Pig Address: Beijing Person2: ID: 002 Name: Sony Pig Address: Shenzhen
View Code

Java Basic Learning--Cloning of objects

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.