Java - 参数声明

时间:2013-05-14 10:08:33

标签: 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();
}

2 个答案:

答案 0 :(得分:6)

它被称为varargs,在这里查看更多http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

  

最终参数类型之后的三个句点表示最终参数可以作为数组或参数序列传递。 Varargs只能在最终的参数位置使用。

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

答案 1 :(得分:3)

参数:

final InetSocketAddress... nodeAddresses

表示变量参数。它可以使用与函数参数相同的数据类型的一个或多个变量。

请参阅:http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html