
Java 執行個體
以下執行個體示範了如何在重載方法中使用可變參數:
public class Main {
static void vaTest(int ... no) {
System.out.print("vaTest(int ...): "
+ "參數個數: " + no.length +" 内容: ");
for(int n : no)
System.out.print(n + " ");
System.out.println();
}
static void vaTest(boolean ... bl) {
System.out.print("vaTest(boolean ...) " +
"參數個數: " + bl.length + " 内容: ");
for(boolean b : bl)
System.out.print(b + " ");
static void vaTest(String msg, int ... no) {
System.out.print("vaTest(String, int ...): " +
msg +"參數個數: "+ no.length +" 内容: ");
for(int n : no)
public static void main(String args[]){
vaTest(1, 2, 3);
vaTest("測試: ", 10, 20);
vaTest(true, false, false);
以上代碼運作輸出結果為:
