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

查看所有标签

Elements of Information Theory

Elements of Information Theory

Thomas M. Cover、Joy A. Thomas / Wiley-Blackwell / 2006-7 / GBP 76.50

The latest edition of this classic is updated with new problem sets and material The Second Edition of this fundamental textbook maintains the book's tradition of clear, thought-provoking instr......一起来看看 《Elements of Information Theory》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试