Java 实例 - 重载(overloading)方法中使用 Varargs
Java 教程
· 2019-02-10 18:28:36
以下实例演示了如何在重载方法中使用可变参数:
Main.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 + " ");
System.out.println();
}
static void vaTest(String msg, int ... no) {
System.out.print("vaTest(String, int ...): " +
msg +"参数个数: "+ no.length +" 内容: ");
for(int n : no)
System.out.print(n + " ");
System.out.println();
}
public static void main(String args[]){
vaTest(1, 2, 3);
vaTest("测试: ", 10, 20);
vaTest(true, false, false);
}
}
以上代码运行输出结果为:
vaTest(int ...): 参数个数: 3 内容: 1 2 3 vaTest(String, int ...): 测试: 参数个数: 2 内容: 10 20 vaTest(boolean ...) 参数个数: 3 内容: true false false
点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html
Concepts, Techniques, and Models of Computer Programming
Peter Van Roy、Seif Haridi / The MIT Press / 2004-2-20 / USD 78.00
This innovative text presents computer programming as a unified discipline in a way that is both practical and scientifically sound. The book focuses on techniques of lasting value and explains them p......一起来看看 《Concepts, Techniques, and Models of Computer Programming》 这本书的介绍吧!