方法签名中的 ... 是什么

发布于 2024-12-26 07:45:21 字数 296 浏览 3 评论 0原文

我第一次在方法签名中看到它 ...

我尝试访问 .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 技术交流群。

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

发布评论

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

评论(6

情泪▽动烟 2025-01-02 07:45:21

该功能称为 Varargs

它允许您向方法提供随机数量的参数。

The feature is called Varargs

It allows you to supply a random number of arguments to a method.

榕城若虚 2025-01-02 07:45:21

它是 varargs并且只能在参数列表的最后使用。最后一个参数可以容纳多个对象。

public class C { 

    int i;
    String[] s;

    public C(int i, String... s){
        this.i = i;
        this.s=s;
    }
}
new C(4,"a","b") // will be transformed to int and String[]

看看“a”和“b”如何转换成数组。

It's varargs and can only be used last in a parameter list. The last param can hold more than one object.

public class C { 

    int i;
    String[] s;

    public C(int i, String... s){
        this.i = i;
        this.s=s;
    }
}
new C(4,"a","b") // will be transformed to int and String[]

See how "a" and "b" has transformed into an array.

燃情 2025-01-02 07:45:21

这是可变参数语法: 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.

猛虎独行 2025-01-02 07:45:21

此符号与

public void addGraphData(GraphData[] _graphData) {

}同义词

This notation synonym for

public void addGraphData(GraphData[] _graphData) {

}

高跟鞋的旋律 2025-01-02 07:45:21

... 表示 < Java 中的代码>varargs
[vararg] 属性指定该方法采用可变数量的参数。要实现此目的,最后一个参数必须是包含所有剩余参数的 VARIANT 类型的安全数组:

[vararg [, optional-attributes]] return-type function-name(
[optional-param-attributes] param-list,
SAFEARRAY(VARIANT) last-param-name);

varargs 语法基本上允许您指定可能的参数,对吧?他们可以在那里,也可以不在那里。这就是三个点的目的。当您调用该方法时,可以使用或不使用这些参数来调用它。这样做是为了避免必须将数组传递给方法。

看看这个

参见< a href="https://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java">你什么时候在Java中使用varargs?

final public class Main
{
    private void show(int []a)
    {
        for(int i=0;i<a.length;i++)
        {
            System.out.print(a[i]+"\t");
        }
    }

    private void show(Object...a)
    {
        for(int i=0;i<a.length;i++)
        {
            System.out.print(a[i]+"\t");
        }

        System.out.println("\nvarargs called");
    }

    public static void main(String... args)  //See also here.
    {
        int[]temp=new int[]{1,2,3,4};            

        Main main=new Main();
        main.show(temp);
        main.show();         //<-- This is possible.
    }
}

正是因为这个原因,基本上不建议在方法重载中使用varargs


System.out.printf();varargs 的示例,定义如下。

public PrintStream printf(String format, Object ... args)
{
     return format(format, args);
}

格式 - 格式字符串语法中所述的格式字符串

args - 格式字符串中的格式说明符引用的参数。如果参数多于格式说明符,则忽略多余的参数。参数的数量是可变的并且可以为零。参数的最大数量受 Java 虚拟机规范定义的 Java 数组的最大维度限制。空参数的行为取决于转换。

... indicates varargs 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 :

[vararg [, optional-attributes]] return-type function-name(
[optional-param-attributes] param-list,
SAFEARRAY(VARIANT) last-param-name);

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?

final public class Main
{
    private void show(int []a)
    {
        for(int i=0;i<a.length;i++)
        {
            System.out.print(a[i]+"\t");
        }
    }

    private void show(Object...a)
    {
        for(int i=0;i<a.length;i++)
        {
            System.out.print(a[i]+"\t");
        }

        System.out.println("\nvarargs called");
    }

    public static void main(String... args)  //See also here.
    {
        int[]temp=new int[]{1,2,3,4};            

        Main main=new Main();
        main.show(temp);
        main.show();         //<-- This is possible.
    }
}

It's for this reason, varargs is basically not recommended in overloading of methods.


System.out.printf(); is an example of varargs and defined as follows.

public PrintStream printf(String format, Object ... args)
{
     return format(format, args);
}

format - A format string as described in Format string syntax

args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.

格子衫的從容 2025-01-02 07:45:21

...表示可变长度参数列表。

printStrings(String... s){
   //internally s is an array with the parameters
   for(int i = 0; i < s.length;++i)
       System.out.print(s[i]);
}

printStrings("A","B","C","D");
//this is done by the compiler
printStrings(new String[]{"A","B","C","D"});

The ... indicates a variable length parameter list.

printStrings(String... s){
   //internally s is an array with the parameters
   for(int i = 0; i < s.length;++i)
       System.out.print(s[i]);
}

printStrings("A","B","C","D");
//this is done by the compiler
printStrings(new String[]{"A","B","C","D"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文