什么是三个点(...)表示作为参数的方法定义中的一部分使用时? [英] What do three dots (...) indicate when used as a part of parameters during method definition?
问题描述
什么方法定义期间参数的一部分时,三个点(...)表示?
What do three dots (...) indicate when used as a part of parameters during method definition?
此外,有没有为那些3点的象征?
Also, is there any programming term for the symbol of those 3 dots?
我一个code样品中发现:
I noticed in a code sample:
public void method1 (Animal... animal) {
// Code
}
这种方法是从2个地方叫。同时呼吁通过的论点在这两种情况下,虽然不同的:
And this method was called from 2 places. The arguments passed while calling were different in both scenarios though:
-
对象的数组作为参数传递给方法一(动物......)
Array of objects is passed as an argument to method1(Animal...)
作为参数传递给方法一类动物的对象(动物......)
Object of class Animal passed as an argument to method1(Animal...)
那么,是不是有点像,如果你不知道你是否会传递数组或整个阵列作为参数传递给方法的一个元素,可以使用3个点作为参数的方法定义的一部分?
So, is it something like, if you are not sure whether you will be passing a single element of an array or the entire array as an argument to the method, you use 3 dots as a part of parameters in the method definition?
另外,请让我知道,如果有是为那些3点的象征任何编程术语。
Also, please let me know if there is any programming term for the symbol of those 3 dots.
推荐答案
这就是所谓的可变参数。
It's called varargs.
这意味着,只要你想,你可以通过该类型的一样多。
It means you can pass as many of that type as you want.
这实际上将其转换为方法1(动物[]一)
,你引用它们为 A [1]
如您在任何其他的阵列。
It actually translates it into method1(Animal[] a)
and you reference them as a[1]
like you would any other array.
如果我有以下
Cat whiskers = new Cat();
Dog rufus = new Dog();
Dolphin flipper = new Dolphin();
method1(whiskers, rufus, flipper); // okay!
method1(rufus); // okay!
method1(); // okay!
method1(flipper,new Parakeet()); // okay!
这篇关于什么是三个点(...)表示作为参数的方法定义中的一部分使用时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!