Java-参数声明

标签 java constructor

我正在研究这段代码,它是一个带有多个参数的构造函数。最后一个参数的声明用...是什么意思?

    /**
 * Public constructor.
 * @param servicePort the service port
 * @param nodeAddresses the node addresses
 * @param sessionAware true if the server is aware of sessions, false otherwise
 * @throws NullPointerException if the given socket-addresses array is null
 * @throws IllegalArgumentException if the given service port is outside range [0, 0xFFFF],
 *    or the given socket-addresses array is empty
 * @throws IOException if the given port is already in use, or cannot be bound
 */
public TcpSwitch(final int servicePort, final boolean sessionAware, final InetSocketAddress... nodeAddresses) throws IOException {
    super();
    if (nodeAddresses.length == 0) throw new IllegalArgumentException();

    this.serviceSocket = new ServerSocket(servicePort);
    this.executorService = Executors.newCachedThreadPool();
    this.nodeAddresses = nodeAddresses;
    this.sessionAware = sessionAware;

    // start acceptor thread
    final Thread thread = new Thread(this, "tcp-acceptor");
    thread.setDaemon(true);
    thread.start();
}

最佳答案

它叫做 varargs,查看这里了解更多 http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.

正如您在代码中看到的,在本例中它是一个数组。

关于Java-参数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16540402/

相关文章:

scala - 带有可变参数的 scala 中的构造函数

java - Java 中具有隐式返回类型的构造函数

c# - 在构造函数中声明的 C# 对象的垃圾收集

java - 渲染纹理不允许其他纹理显示 libgdx java

JavaFx 不接受媒体源文件

java - ARM64 机器上的 Cassandra 启动失败(java.lang.NoClassDefFoundError : Could not initialize class com. sun.jna.Native)

java - 将 Java 代理检测传输到生成的 JVM

构造函数中的 C++ 初始化列表

java - 显示准确时间而不是设备时间

java - 在 Swing 中按下按钮之前阻止流程继续进行