java 对象的正确术语... args
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16674023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper terminology for Object... args
提问by SnakeDoc
I'm working in Java and the typical way you specify multiple args for a method is:
我在 Java 中工作,为一个方法指定多个 args 的典型方法是:
public static void someMethod(String[] args)
But, I've seen another way a few times, even in the standard Java library. I don't know how to refer to this in conversation, and googling is not of much help due to the characters being used.
但是,即使在标准 Java 库中,我也见过几次另一种方式。我不知道如何在对话中提及这个,而且由于使用了字符,谷歌搜索也没有太大帮助。
public static void someMethod(Object... args)
I know this allows you to string a bunch or arguments into a method without knowing ahead of time exactly how many there might be, such as:
我知道这允许您将一堆或参数串到一个方法中,而无需提前知道可能有多少,例如:
someMethod(String arg1, String arg2, String arg3, ... etc
How do you refer to this type of method signature setup? I think it's great and convenient and want to explain it to some others, but am at a lack of how to refer to this. Thank you.
您如何看待这种类型的方法签名设置?我认为它很棒也很方便,想向其他人解释一下,但我不知道如何引用它。谢谢你。
采纳答案by Keppil
回答by Dan
As a Keppil and Steve Benett pointed out that this java feature is called varargs.
正如 Keppil 和 Steve Benett 指出的那样,这个 java 特性被称为 varargs。
If I'm not mistaken, Joshua Bloch mentioned that there is a performance hit for using varargs and recommends telescoping and using the varargs method as a catch all sink.
如果我没记错的话,Joshua Bloch 提到使用 varargs 会影响性能,并建议伸缩并使用 varargs 方法作为捕获所有接收器。
public static void someMethod(Object arg) {
// Do something
}
public static void someMethod(Object arg1, Object arg2) {
// Do something
}
public static void someMethod(Object arg1, Object arg2, Object arg3) {
// Do something
}
public static void someMethod(Object ... args) {
// Do something
}