方法签名中的 ... 是什么
我第一次在方法签名中看到它 ...
。
我尝试访问 .class 文件。它有一个定义如下的方法
public void addGraphData(GraphData... _graphData) {
}
并且 GraphData 只是带有 getter 和 setter 的 POJO。为什么 .class 文件显示 GraphData..._graphData
而不是 GraphData _graphData
?
I have seen it first time ...
in a method signature.
I tried to access a .class file. It has a method defined as below
public void addGraphData(GraphData... _graphData) {
}
And that GraphData is nothing but POJO with getters and setters. Why is the .class file displaying GraphData... _graphData
instead of GraphData _graphData
??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
该功能称为 Varargs
它允许您向方法提供随机数量的参数。
The feature is called Varargs
It allows you to supply a random number of arguments to a method.
它是 varargs并且只能在参数列表的最后使用。最后一个参数可以容纳多个对象。
看看“a”和“b”如何转换成数组。
It's varargs and can only be used last in a parameter list. The last param can hold more than one object.
See how "a" and "b" has transformed into an array.
这是可变参数语法: http://docs.oracle .com/javase/1.5.0/docs/guide/language/varargs.html
它被视为 GraphData[],可以作为可扩展参数动态构建。 Arrays.asList() 是另一个这样的例子。
That is the varargs syntax: http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
It's treated like a GraphData[] which can be build on the fly as extensible parameters. Arrays.asList() is another such example.
此符号与
public void addGraphData(GraphData[] _graphData) {
}同义词
This notation synonym for
public void addGraphData(GraphData[] _graphData) {
}
...
表示 < Java 中的代码>varargs。[vararg]
属性指定该方法采用可变数量的参数。要实现此目的,最后一个参数必须是包含所有剩余参数的 VARIANT 类型的安全数组:varargs 语法基本上允许您指定可能的参数,对吧?他们可以在那里,也可以不在那里。这就是三个点的目的。当您调用该方法时,可以使用或不使用这些参数来调用它。这样做是为了避免必须将数组传递给方法。
看看这个:
参见< a href="https://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java">你什么时候在Java中使用varargs?
正是因为这个原因,基本上不建议在方法重载中使用
varargs
。System.out.printf();
是varargs
的示例,定义如下。...
indicatesvarargs
in Java.The
[vararg]
attribute specifies that the method takes a variable number of parameters. To accomplish this, the last parameter must be a safe array of VARIANT type that contains all the remaining parameters :The varargs syntax basically lets you specify that there are possible parameters, right? They can be there, or cannot be there. That's the purpose of the three dots. When you call the method, you can call it with or without those parameters. This was done to avoid having to pass arrays to the methods.
Have a look at this:
See When do you use varargs in Java?
It's for this reason,
varargs
is basically not recommended in overloading of methods.System.out.printf();
is an example ofvarargs
and defined as follows....
表示可变长度参数列表。The
...
indicates a variable length parameter list.