class Te
{
public static int m=1; public static void main(String[] args)
{
new Te().execute();
} private void execute() {
//AAA aAAA=new AAA();
AAA.aaa(m);
System.out.println(m);
}
} class AAA
{
static int aaa(int x){
x=++x;
System.out.println(x);
return x;
}
}结果为什么是:
2
1为什么不是:
2
2
呢?
如果想变成:
2
2
该怎么改?
谢谢!
{
public static int m=1; public static void main(String[] args)
{
new Te().execute();
} private void execute() {
//AAA aAAA=new AAA();
AAA.aaa(m);
System.out.println(m);
}
} class AAA
{
static int aaa(int x){
x=++x;
System.out.println(x);
return x;
}
}结果为什么是:
2
1为什么不是:
2
2
呢?
如果想变成:
2
2
该怎么改?
谢谢!
传进去的直接就是x的值1,而不是x.
也就是说在方法中改变的是1,而不是x
//AAA aAAA=new AAA();
m = AAA.aaa(m);
System.out.println(m); }
编译期间已经初始化m值为1,
调用Te默认构造函数之后进入execute方法,
先调用AAA.aaa(m);
对x先加加成2之后打印输出2,
回到execute方法中,
打印m的值为1因为m传递到aaa方法中的传递过程是值传递,修改x并不会改变m的值基础啊!
{
public static int m=1; public static void main(String[] args)
{
new Te().execute();
} private void execute() {
//AAA aAAA=new AAA();
AAA.aaa(new Te());
System.out.println(m); }
}
class AAA
{
static int aaa(Te n ){
n.m=++n.m;
System.out.println(n.m);
return n.m;
}
}
小弟初步领会了!
现在去网上查关于参数传递问题!
2
1了
如果你想要
2
2
那private void execute() {
//AAA aAAA=new AAA();
m = AAA.aaa(m);
System.out.println(m); } 应该改写成
private void execute() {
//AAA aAAA=new AAA();
System.out.println(AAA.aaa(m)); }