Java Clone method Lazy Person implementation __java

Source: Internet
Author: User
Tags getmessage memory usage serialization

The (Protected) clone () method is defined in the Java object class, and if its own class requires a Clone method, the Cloneable interface is implemented, the Clone () method is overridden, and the method access level is changed to (public). However, if your own class if the property is more, rewriting the Clone method will still take a lot of time, more importantly, later add or delete attributes should also modify the Clone method, in general, it is more troublesome.

Here, if you're not too sure about performance, there's a quick and easy way to implement the Clone method, which is to use the serialization functionality of the Java language to implement the Clone method, as follows:

Here are the bean classes for several tests

Import java.io.Serializable; public class A implements Serializable {private static final long serialversionuid = 1L; private String name; private b b ; Public String GetName () {return name.} public void SetName (String name) {this.name = name:} public B Getb () {return B ; The public void Setb (b b) {this.b = b.} @Override public Object clone () {return cloneutil.clone (this);} Import java.io.Serializable; public class B implements Serializable {private static final long serialversionuid = 1L; private String name; private C C ; Public String GetName () {return name.} public void SetName (String name) {this.name = name:} public C getc () {return C ; public void SetC (c c) {this.c = c.} @Override Public Object clone () {return cloneutil.clone (this);} Import java.io.Serializable; public class C implements Serializable, cloneable {private static final long serialversionuid = 1L; private String name; Public String GetName () {return name.} public void SetName (String name) {thiS.name = name; @Override public Object Clone () {return cloneutil.clone (this);}}  

The Clone tool class, which is responsible for cloning an object by serializing and deserializing

Import java.io.*; public class Cloneutil {public static object clone (Object obj) {object anotherobj = null; byte[] bytes; Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); ObjectOutputStream oos = null; try {oos = new ObjectOutputStream (BAOs); Oos.writeobject (obj); bytes = Baos.tobytearray ();} catch (IOException ex) {thro W New RuntimeException (Ex.getmessage (), ex); finally {if (Oos!= null) {try {oos.close ();} catch (IOException e) {//Ignore Me}}} Bytearrayinputstream Bais = New Bytearrayinputstream (bytes); ObjectInputStream ois = null; try {ois = new ObjectInputStream (Bais); anotherobj = Ois.readobject ();} catch (IOException ex) {throw new Runtimeexcepti On (Ex.getmessage (), ex); The catch (ClassNotFoundException ex) {throw new RuntimeException (Ex.getmessage (), ex);} finally {if (ois!= null) {try { Ois.close (); catch (IOException e) {//Ignore me}} return anotherobj; } }

Test class

public class Test {public static void main (string[] args) throws Exception {A A = new A (); b b = new B (); c C = new C (); C.setname ("CCC"); B.setname ("BBB"); B.setc (c); A.setname ("AAA"); A.setb (b); System.out.println ("A:" + a); System.out.println ("A:" + a.getname ()); System.out.println ("A:" + A.GETB (). GetName ()); System.out.println ("A:" + A.GETB (). GETC (). GetName ()); A anothera = (a) a.clone (); System.out.println ("Anothera:" + Anothera); System.out.println ("Anothera:" + anothera.getname ()); System.out.println ("Anothera:" + ANOTHERA.GETB (). GetName ()); System.out.println ("Anothera:" + ANOTHERA.GETB (). GETC (). GetName ()); System.out.println ("= = = Change properties of a = = ="); A.setname ("AAAAA"); A.getb (). SetName ("bbbbb"); A.getb (). GETC (). SetName ("CCCCC"); System.out.println ("A:" + a); System.out.println ("A:" + a.getname ()); System.out.println ("A:" + A.GETB (). GetName ()); System.out.println ("A:" + A.GETB (). GETC (). GetName ()); System.out.println ("Anothera:" + Anothera); System.out.prIntln ("Anothera:" + anothera.getname ()); System.out.println ("Anothera:" + ANOTHERA.GETB (). GetName ()); System.out.println ("Anothera:" + ANOTHERA.GETB (). GETC (). GetName ()); System.out.println ("= = = Change properties of Anothera = ="); Anothera.setname ("AAAA"); Anothera.getb (). SetName ("bbbb"); Anothera.getb (). GETC (). SetName ("CCCC"); System.out.println ("A:" + a); System.out.println ("A:" + a.getname ()); System.out.println ("A:" + A.GETB (). GetName ()); System.out.println ("A:" + A.GETB (). GETC (). GetName ()); System.out.println ("Anothera:" + Anothera); System.out.println ("Anothera:" + anothera.getname ()); System.out.println ("Anothera:" + ANOTHERA.GETB (). GetName ()); System.out.println ("Anothera:" + ANOTHERA.GETB (). GETC (). GetName ());}  

Run the test class to see the results. The code here shows that there are some limitations to this implementation:

1. Its own bean must implement the Serializable interface;

2. Because this implementation uses serialization, the performance is not very good, so if there are too many objects, it is not recommended;

3. Because the object is not serialized into a file in the serialization process, it is kept in an array of memory, so if the object is too large, it will cause a larger memory usage, which requires attention.

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.