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

一例子,帮分析
刚接触JAVA的形参实参,这例子怎样传值以及怎样运算不是太懂,麻烦帮分析,越详细越好,谢谢了
public class Mix4 
{
int counter = 0;

public int maybeNew(int index)
{
if(index < 5)
{
Mix4 m4 = new Mix4();
m4.counter = m4.counter + 1;
return 1;
}
return 0;
}

public static void main(String[] args)
{
int count = 0;
Mix4[] m4a = new Mix4[20];
int x = 0;
while(x < 9)
{
m4a[x] = new Mix4();
m4a[x].counter = m4a[x].counter + 1;
count =count + 1;
count = count + m4a[x].maybeNew(x);
x = x + 1;
}
System.out.println(count + " " + m4a[1].counter);
}


}

------解决方案--------------------
count = count + m4a[x].maybeNew(x); 这里的x是实参 传给了形参index
------解决方案--------------------
count = count + m4a[x].maybeNew(x); //这里传入x,返回int 加上count 赋值给count