public class TestClone implements Cloneable{

  int count;
  TestClone next;
  public TestClone(int count){
  
   this.count=count;
   if(count>0)                    //递归 一连串的 next 对象??
       next=new TestClone(count-1);
                           }
                   
     void add(){          
        count++;
        if(next!=null)    //怎么循环??弄不懂他的联系机制,如c/c++里有指针,那么这里??
        next.count++;     //就是说他怎么传递下去的
               }
 public String toString(){  //为什么制动调用这个方法????
 
  String s=String.valueOf(count)+"";
  if(next!=null)          //这里也一样???
  s+=next.toString();
  return s;
                     }
                     
                     
 public Object clone(){
 
  Object o=null;
  try{
  o=super.clone();
 
  }catch(Exception e)
  {System.out.println("cannot clone");}
  return o;
                  }                                                         
public static void main(String []args){

TestClone c=new TestClone(5);
          
          System.out.println("c="+c);//输出为5 4 3 2 1 0 怎么能过输出对象C??
          TestClone cl=(TestClone)c.clone();
          System.out.println("c="+c);//输出为5 4 3 2 1 0
          c.add();
          
          System.out.println("after added\nc="+c+"\ncl="+"cl");//输出为6 5 3 2 1 0 四到那里去了??
                                 }                           //     5 4 3 2 1 0 
}

解决方案 »

  1.   

    答案我看好像很正确啊在第一次输出的时候,c的值为5,而c的next对象的值为4,而c的next对象的next对象的值为3,这么一直下去,直到0的时候,就没有next对象了,所以输出5 4 3 2 1 0在第二次输出的时候由于c1是c对象clone而来,TestClone中的clone方法只是把Object的clone方法再运行了一次,所以c1和c对象内容是一样的,c1对象中的next对象其实和刚才c对象中的next对象是一个对象。所以输出的结果还是5 4 3 2 1 0.在add方法中,紧紧是把c的值还有c的next对象的值加了1,而并没有循环调用,所以只是 5和4两个加了一,其他没有变动,所以最后结果是6 5 3 2 1 0
      

  2.   

    建议楼主看一下tij appendex A
      

  3.   

    其他的懂了 就是:
     public String toString(){  //为什么制动调用这个方法????
     
      String s=String.valueOf(count)+"";
      if(next!=null)          
      s+=next.toString();
      return s;
                         }
    但是 main 函数里面并没有调用这个方法呀????另外什么是:  tij appendex A   ??
      

  4.   

    thinking in java 中的代码appendex A