Java学习----你可以知道对象的工作结果(获取方法的返回值)

时间:2021-12-26 23:14:12

1.写返回类型

2.return 返回值

3.定义变量接受返回值

public class App2 {
public String [] print(String msg, int num) {
for (int i = 0; i < num; i++) {
System.out.println(msg);
}
return new String[]{"aaa","bbb"};
} public static void main(String[] args) {
App2 obj = new App2();
String[] x = obj.print("hello world", 5);
System.out.println(x[0]);
System.out.println(x[1]);
}
}

执行结果:

hello world
hello world
hello world
hello world
hello world
aaa
bbb