日期:2014-05-20  浏览次数:20703 次

阿里巴巴的笔试题,请大神给我解释下n的值。
请写出下面程序的输出:
public class Test {
public static int k=0;
public static Test t1=new Test("t1");
public static Test t2=new Test("t2"); 
public static int i=print("i");
public static int n=99;
public int j=print("j");
{
System.out.print("构造块");
}
static{
System.out.print("静态构造块");
}
public Test(String string) {
System.out.println((++k)+":"+string+"   i="+i+"   n="+n);
++i;++n;
}
private static int print(String string) {
System.out.println((++k)+":"+string+"   i="+i+"   n="+n);
++n;
return ++i;
}
public static void main(String[] args) {
new Test("init");
}
下面是输出:
1:j   i=0   n=0
构造块2:t1   i=1   n=1
3:j   i=2   n=2
构造块4:t2   i=3   n=3
5:i   i=4   n=4
静态构造块6:j   i=5   n=99
构造块7:init   i=6   n=100
我的答案n的值是从99-105,求解释一下,谢谢啊 分不多了啊.
笔试题 类的初始化

------解决方案--------------------
第四次出现在该论坛上了,加上2008年那一次,我统计的应该是第五次了。。。
第一次
http://bbs.csdn.net/topics/390597457
第二次
http://bbs.csdn.net/topics/390605401
第三次
http://bbs.csdn.net/topics/390606269
你这是第四次问。。。
这个题在2008年的时候就有人问了。。还是被加了精华的帖子
http://bbs.csdn.net/topics/280036316
关于这个题,我你应该先搜索一下帖子再问。。。