0

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

    /**
 * 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();
}
4

2 回答 2

6

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

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

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

于 2013-05-14T10:10:09.197 回答
3

参数:

final InetSocketAddress... nodeAddresses

表示可变参数。它可以将 1 个或多个具有相同数据类型的变量作为函数的参数。

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

于 2013-05-14T10:10:56.223 回答