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

查看所有标签

XMPP

XMPP

Peter Saint-Andre、Kevin Smith、Remko TronCon / O'Reilly Media / 2009-5-4 / USD 39.99

This practical book provides everything you need to know about the Extensible Messaging and Presence Protocol (XMPP). This open technology for real-time communication is used in many diverse applicati......一起来看看 《XMPP》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码