775ed8d4abe22dffadb4db7f5e4c85e1bb969dec.svn-base 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package samples.technology.reference0;
  2. class CloneC implements Cloneable{
  3. public String str;
  4. public StringBuffer strBuff;
  5. public Object clone(){
  6. CloneC o = null;
  7. try{
  8. o = (CloneC)super.clone();
  9. }catch(CloneNotSupportedException e){
  10. e.printStackTrace();
  11. }
  12. return o;
  13. }
  14. }
  15. public class StrClone {
  16. public static void main(String[] a){
  17. CloneC c1 = new CloneC();
  18. c1.str = new String("initializeStr");
  19. c1.strBuff = new StringBuffer("initializeStrBuff");
  20. System.out.println("before clone,c1.str = "+ c1.str);
  21. System.out.println("before clone,c1.strBuff = "+ c1.strBuff);
  22. CloneC c2 = (CloneC)c1.clone();
  23. //c2.str = c2.str.substring(0,5);
  24. c2.str = "dsafasdf";
  25. c2.str.substring(0,5);
  26. c2.strBuff = c2.strBuff.append(" change strBuff clone");
  27. System.out.println("=================================");
  28. System.out.println("after clone,c1.str = "+ c1.str);
  29. System.out.println("after clone,c1.strBuff = "+ c1.strBuff);
  30. System.out.println("=================================");
  31. System.out.println("after clone,c2.str = "+ c2.str);
  32. System.out.println("after clone,c2.strBuff = "+ c2.strBuff);
  33. }
  34. }