java 回传参数

时间:2023-03-09 20:50:00
java 回传参数

通过 new 创建的对象可以实现回传,如数组;自定义类对象里的参数。

【数组方式】

public static void main(String[] args)
{
try
{
int [] amount= new int[1];

amount[0] =1;

Test2(amount);
// 将显示 amount的赋值后的结果99  
System.out.print(amount[0]);
}
catch (Exception e)
{
e.printStackTrace();
}
}

private static void Test2(int[] amount) throws Exception
{
amount[0] = 99;
}

【自定义类对象方式】

// 函数传入自定义类,测试回传
// -------------------------------------------
MyParam param= new MyParam ();
param.a = 0;
param.b = 0;
// 引用有效
setValue(param);   //在函数里面对param的a和b赋值操作
// 得到赋值后的结果
System.out.println(value.a);
System.out.println(value.b);