如何从 Java 方法返回 2 个值
如果我们向方法传递一个对象,然后修改其值,则该方法可以提供多个值。请参见以下示例 -
示例
public class Tester { public static void main(String[] args) { Model model = new Model(); model.data1 = 1; model.data2 = 2; System.out.println(model.data1 + ", " + model.data2); changeValues(model); System.out.println(model.data1 + ", " + model.data2); } public static void changeValues(Model model) { model.data1 = 100; model.data2 = 200; } } class Model { int data1; int data2; }
输出
1, 2 100, 200
广告