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

查看所有标签

Spark

Spark

Bill Chambers、Matei Zaharia / O′Reilly / 2017-10-31 / GBP 39.99

Learn how to use, deploy, and maintain Apache Spark with this comprehensive guide, written by the creators of the open-source cluster-computing framework. With an emphasis on improvements and new feat......一起来看看 《Spark》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具