import java.util.*;
class Gerbil {
private static int gerbilNumber=1;
public Gerbil() {
gerbilNumber++;
}
public void hop(){
System.out.println(this.gerbilNumber +" gerbil is hoping");
}
}
public class GerbilTest {
public static void main(String[] args) {
ArrayList a=new ArrayList();
for(int i=0;i<10;i++){
a.add(new Gerbil());
}
for(int i=0;i<10;i++){
((Gerbil)a.get(i)).hop();
}
}
}
输出结果竟然是
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
11 gerbil is hoping
我打算输出的是 1 2 3 4 5 6 7 8 9 10 啊怎么是11
而且在eclips中这两个类必须在两个文件中然后((Gerbil)a.get(i)).hop();报错,说Gerbil无法识别 期待帮助