public class Snake implements Cloneable{
private Snake next;
private char c;
public Snake(int i,char x) {
c = x;
if(--i > 0){
next = new Snake(i,(char)(x+1));
}
}
void increment(){
c ++;
if(next != null){
next.increment();
}
}
public String toString(){
String s = ":" +c;
if(next != null){
s+= next.toString();
}
return s;
}
public Object clone(){
Object o = null;
try{
o = super.clone();
}catch(Exception e){
}
return o;
}
public static void main(String[] agrs){
Snake s = new Snake(5,'a');
System.out.println("s = "+ s.toString());
}
}
以上程序运行结果为 s = :a:b:c:d:e
本人想知道为什么 重复给一个next赋值,可以有5个对象,还有toString输出的时候
为什么是一个 字符串 而不是单个字符 比如 :a,因为我从他这里没看出任何循环。