此方法签名中的省略号 (...) 代表什么?
在 App Engine 文档,这个方法签名中的省略号(JID...
)代表什么?
public MessageBuilder withRecipientJids(JID... recipientJids)
这三个点的作用是什么?
In the App Engine docs, what is the ellipsis (JID...
) for in this method signature?
public MessageBuilder withRecipientJids(JID... recipientJids)
What's the function of those three dots?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这些是 Java 可变参数。它们允许您传递任意数量的特定类型的对象(在本例中它们是 JID 类型)。
在您的示例中,以下函数调用将是有效的:
请在此处查看更多信息:
http://java.sun.com/j2se/1.5 .0/docs/guide/language/varargs.html
Those are Java varargs. They let you pass any number of objects of a specific type (in this case they are of type JID).
In your example, the following function calls would be valid:
See more here:
http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html
在方法内使用省略号或可变参数的方式就好像它是一个数组:
可以按如下方式调用该方法:
在
PrintWithEllipsis
内部,setOfStrings
的类型是一个 String 数组。因此,您可以节省编译器的一些工作并传递一个数组:
对于 varargs 方法,序列参数被视为相同类型的数组。因此,如果两个签名的不同之处仅在于一个声明一个序列,另一个声明一个数组,如本示例所示:
则会发生编译时错误。
来源:Java 编程语言规范,其中技术术语是
变量参数< /code> 而不是常用术语
varargs
。The way to use the ellipsis or varargs inside the method is as if it were an array:
This method can be called as following:
Inside
PrintWithEllipsis
, the type ofsetOfStrings
is an array of String.So you could save the compiler some work and pass an array:
For varargs methods, a sequence parameter is treated as being an array of the same type. So if two signatures differ only in that one declares a sequence and the other an array, as in this example:
then a compile-time error occurs.
Source: The Java Programming Language specification, where the technical term is
variable arity parameter
rather than the common termvarargs
.三点(...)符号实际上是从数学借用的,意思是“...等等”。
至于它在Java中的使用,它代表
varargs
,意味着可以在方法调用中添加任意数量的参数。唯一的限制是varargs
必须位于方法签名的末尾,并且每个方法只能有一个。The three dot (...) notation is actually borrowed from mathematics, and it means "...and so on".
As for its use in Java, it stands for
varargs
, meaning that any number of arguments can be added to the method call. The only limitations are that thevarargs
must be at the end of the method signature and there can only be one per method.这些是
varargs
它们用于创建接收任意数量参数的方法。例如 PrintStream.printf 方法使用它,因为您不知道将使用多少个参数。
它们只能用作参数的最终位置。
varargs
已添加到 Java 1.5Those are
varargs
they are used to create a method that receive any number of arguments.For instance PrintStream.printf method uses it, since you don't know how many would arguments you'll use.
They can only be used as final position of the arguments.
varargs
was was added on Java 1.5这意味着该方法接受 可变数量的参数< /a>(“varargs”),类型为
JID
。在该方法中,显示了recipientJids
。如果您有一种方法可以选择以一种自然的方式处理多个参数,并且允许您编写可以将一个、两个或三个参数传递给同一方法的调用,而无需创建丑陋的代码,那么这非常方便。动态数组。
它还支持 C 语言中的诸如
sprintf
之类的习惯用法;例如,请参见String.format()
。It means that the method accepts a variable number of arguments ("varargs") of type
JID
. Within the method,recipientJids
is presented.This is handy for cases where you've a method that can optionally handle more than one argument in a natural way, and allows you to write calls which can pass one, two or three parameters to the same method, without having the ugliness of creating an array on the fly.
It also enables idioms such as
sprintf
from C; seeString.format()
, for example.