123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package samples.technology.reference1;
- 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
- */
|