如何从 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
广告