public class Ne { /**
 * @param args
 */
//StringBuffer bu = new StringBuffer(" ");
private static int count = 0;
private final int id = count++;
public String toString(){
return id+" ";
}
public static void main(String[] args) {
// TODO 自动生成方法存根
Ne n = new Ne();
for(int i=0;i<5;i++){
System.out.print(new Ne());

}
System.out.println();
for(int i=0;i<5;i++){
System.out.print(n);
} }}
输出结果:1 2 3 4 5 
          0 0 0 0 0
为什么??????????

解决方案 »

  1.   

    虽然输出的是12345 有一定的规律 但这时的12345不代表数字 
    你输出new Ne()没有意义
      

  2.   

    (1)引文count是static,所以每new一次,count++;
    Ne n = new Ne();这句n.id=0;System.out.print(n)没有new对象,所以是0 0 0 0 0。
       (2)for(int i=0;i<5;i++){
                System.out.print(new Ne());
                
            }
    每循环一次,new一个,所以1 2 3 4 5 
      

  3.   

    private static int count = 0;
    private final int id = count++;count 是static的,被所有的Ne的对象共享,
    你创建第一个对象时,count = 0,id =,0;
    再执行private final int id = count++; 之后, count的值加1,id的值不变,还是0,因为是常量。
    创建第二个对象时,count的初始值已经是1了,所以count = 1,id = 1;
    再执行private final int id = count++;之后  count的值加1, id的值仍不变,还是1;
    创建第三个对象时,count的初始值已经是2,所以count = 2,id = 2;
    再执行private final int id = count++;之后  count的值加1, id的值仍不变,还是2;依次。。
    所以1,2,3,4,5是你创建的第二、三、四、五、六个对象的toString执行的结果而0,0,0,0,0  是第一个对象的toString循环执行的结果
      

  4.   

    另附 其实是1、4、5楼扯淡,直接print一个对象实际上是print该对象toString方法产生的字符串
      

  5.   

    Ne 是一个对象实例,并不是什么具体变量,它怎么可能打印出来东西啊,如果你这样打印,如:
    n.id,或者n.count,就会有所指了