Package clone.ext; Class Unclonea implements cloneable{ private int i; Public Unclonea (int ii) {i = II} public void Doublevalue () {i *= 2;} Public String toString () { return integer.tostring (i); } Public Object Clone () { Unclonea o = null; try{ o = (unclonea) super.clone (); }catch (Clonenotsupportedexception e) { E.printstacktrace (); } return o; } } Class Cloneb implements cloneable{ public int aint; Public Unclonea Unca = new Unclonea (111); Public Object Clone () { Cloneb o = null; try{ o = (cloneb) super.clone (); }catch (Clonenotsupportedexception e) { E.printstacktrace (); } O.unca = (unclonea) unca.clone (); return o; } } public class Clonemain { public static void Main (string[] a) { Cloneb B1 = new Cloneb (); B1.aint = 11; System.out.println ("Before clone,b1.aint =" + B1.aint); System.out.println ("Before Clone,b1.unca =" + B1.unca); Cloneb b2 = (cloneb) b1.clone (); B2.aint = 22; B2.unCA.doubleValue (); System.out.println ("================================="); System.out.println ("After clone,b1.aint =" + B1.aint); System.out.println ("After Clone,b1.unca =" + B1.unca); System.out.println ("================================="); System.out.println ("After clone,b2.aint =" + B2.aint); System.out.println ("After Clone,b2.unca =" + B2.unca); } } /** RUN Result: Before clone,b1.aint = 11 Before Clone,b1.unca = 111 ================================= After Clone,b1.aint = 11 After Clone,b1.unca = 111 ================================= After Clone,b2.aint = 22 After Clone,b2.unca = 222 */ |