A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© dengxuye 中级黑马   /  2016-3-23 21:43  /  474 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Test{
static int i;
public int aMethod( ){
i++;
return i;
}
public static void main(String [] args){
Test test = new Test( );
test.aMethod( );
System.out.println(test.aMethod( ));
}
}
最后输出的是2.求大神分析过程

4 个回复

倒序浏览
i的初始化值是0,调用一次aMethod,i就+1,你调用了2次,返回2.有什么问题啊?
回复 使用道具 举报
test.aMethod( );执行一次aMethod方法,i变成1
System.out.println(test.aMethod( )); 又执行一次aMethod方法,i变成2
回复 使用道具 举报
ShallDoll 发表于 2016-3-23 21:53
i的初始化值是0,调用一次aMethod,i就+1,你调用了2次,返回2.有什么问题啊? ...

明白!谢谢啦!
回复 使用道具 举报
妄想年少轻狂 发表于 2016-3-23 22:34
test.aMethod( );执行一次aMethod方法,i变成1
System.out.println(test.aMethod( )); 又执行一次aMethod方 ...

明白了,谢谢大神!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马