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

查看所有标签

Is Parallel Programming Hard, And, If So, What Can You Do About

Is Parallel Programming Hard, And, If So, What Can You Do About

Paul E. McKenney

The purpose of this book is to help you understand how to program shared-memory parallel machines without risking your sanity.1 By describing the algorithms and designs that have worked well in the pa......一起来看看 《Is Parallel Programming Hard, And, If So, What Can You Do About 》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

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

多种字符组合密码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具