此方法签名中的省略号 (...) 代表什么?

发布于 2024-08-24 03:39:14 字数 309 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

亚希 2024-08-31 03:39:14

这些是 Java 可变参数。它们允许您传递任意数量的特定类型的对象(在本例中它们是 JID 类型)。

在您的示例中,以下函数调用将是有效的:

MessageBuilder msgBuilder; //There should probably be a call to a constructor here ;)
MessageBuilder msgBuilder2;
msgBuilder.withRecipientJids(jid1, jid2);
msgBuilder2.withRecipientJids(jid1, jid2, jid78_a, someOtherJid);

请在此处查看更多信息:
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:

MessageBuilder msgBuilder; //There should probably be a call to a constructor here ;)
MessageBuilder msgBuilder2;
msgBuilder.withRecipientJids(jid1, jid2);
msgBuilder2.withRecipientJids(jid1, jid2, jid78_a, someOtherJid);

See more here:
http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

深白境迁sunset 2024-08-31 03:39:14

在方法内使用省略号可变参数的方式就好像它是一个数组:

public void PrintWithEllipsis(String...setOfStrings) {
    for (String s : setOfStrings)
        System.out.println(s);
}

可以按如下方式调用该方法:

obj.PrintWithEllipsis(); // prints nothing
obj.PrintWithEllipsis("first"); // prints "first"
obj.PrintWithEllipsis("first", "second"); // prints "first\nsecond"

PrintWithEllipsis内部,setOfStrings 的类型是一个 String 数组。
因此,您可以节省编译器的一些工作并传递一个数组:

String[] argsVar = {"first", "second"};
obj.PrintWithEllipsis(argsVar);

对于 varargs 方法,序列参数被视为相同类型的数组。因此,如果两个签名的不同之处仅在于一个声明一个序列,另一个声明一个数组,如本示例所示:

void process(String[] s){}
void process(String...s){}

则会发生编译时错误。

来源:Java 编程语言规范,其中技术术语是变量参数< /code> 而不是常用术语 varargs

The way to use the ellipsis or varargs inside the method is as if it were an array:

public void PrintWithEllipsis(String...setOfStrings) {
    for (String s : setOfStrings)
        System.out.println(s);
}

This method can be called as following:

obj.PrintWithEllipsis(); // prints nothing
obj.PrintWithEllipsis("first"); // prints "first"
obj.PrintWithEllipsis("first", "second"); // prints "first\nsecond"

Inside PrintWithEllipsis, the type of setOfStrings is an array of String.
So you could save the compiler some work and pass an array:

String[] argsVar = {"first", "second"};
obj.PrintWithEllipsis(argsVar);

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:

void process(String[] s){}
void process(String...s){}

then a compile-time error occurs.

Source: The Java Programming Language specification, where the technical term is variable arity parameter rather than the common term varargs.

相权↑美人 2024-08-31 03:39:14

三点(...)符号实际上是从数学借用的,意思是“...等等”。

至于它在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 the varargs must be at the end of the method signature and there can only be one per method.

凝望流年 2024-08-31 03:39:14

这些是 varargs 它们用于创建接收任意数量参数的方法。

例如 PrintStream.printf 方法使用它,因为您不知道将使用多少个参数。

它们只能用作参数的最终位置。

varargs 已添加到 Java 1.5

Those 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

游魂 2024-08-31 03:39:14

这意味着该方法接受 可变数量的参数< /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; see String.format(), for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文