Java
三个点
...
支持函数接收不定参数和数组
Java >= 1.5
class Demo{
public static void showArgs(int ...args){
for(int arg : args){
System.out.println(arg);
}
}
public static void main(String[] args) {
// 1、传入不定参数
showArgs(1, 2, 3);
// 1 2 3
// 2、传入数组
int[] list = new int[]{1, 2, 3};
showArgs(list);
// 1 2 3
}
}
Python
使用
*
接收不定参数和列表解包操作
def showArgs(*args):
for arg in args:
print(arg)
def main():
# 1、传入不定参数
showArgs(1, 2, 3)
# 1 2 3
lst = [1, 2, 3]
# 2、传入列表
showArgs(lst)
# [1, 2, 3]
# 3、将列表解包后传入
showArgs(*lst)
# 1 2 3
if __name__ == '__main__':
main()
PHP
PHP 5.6+
3个点
...
可以接收不定参数和数组解包
<?php
function showArgs(...$args){
foreach ($args as $arg) {
echo $arg . PHP_EOL;
}
}
// 1、接收不定参数
showArgs(1, 2, 3);
// 1 2 3
// 2、接收数组
$list = [1, 2, 3];
showArgs($list);
// Array
// 3、数组解包后传入
showArgs(...$list);
// 1 2 3
JavaScript
...
function showArgs(...args){
for(arg of args){
console.log(arg);
}
}
// 1、传入不定参数
showArgs(1, 2, 3);
//1 2 3
list = [1, 2, 3];
// 2、传入列表
showArgs(list);
// [ 1, 2, 3 ]
// 3、传入解包后的列表
showArgs(...list);
//1 2 3