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个点...
可以接收不定参数和数组解包
关注
打赏