定義了個 Result 類,裡面設定了多種不同類型的屬性。
後面函數隻要把想傳回的值存儲在 Result 對象裡再傳回這個對象,就可以實作函數傳回多個不同類型的值的需求了。
package com.test.test;
public class test {
// 定義一個類,包含int和String數組兩個屬性
private static class Result {
int a;
String[] b;
}
public static void main(String[] args){
// 擷取函數傳回的資料
Result r = getValue();
// 展示傳回的結果
System.out.println(r.a);
System.out.println(r.b[0]);
}
// 文本處理,傳回一個對象
private static Result getValue() {
// 建立對象
Result r = new Result();
// 要傳回的資料
int a = 8;
String[] b = new String[1000];
b[0] = "A01";
b[1] = "A02";
// 将int和String數組的值傳給對象
r.a = a;
r.b = b;
return r;
}
}
運作效果圖:

喜歡的點個贊❤吧!