java中man,什么是“公共天氣預報員(整數...拉鏈){”在java中的含義

I am trying to read some Java code from a tutorial, I don't understand the line:

我試圖從教程中讀取一些Java代碼,我不明白這一行:

public Weatherman(Integer... zips) {

I don't understand what the ... represents if it was just (Integer zips) I would understand that there is a variable of class Integer called zips. But the ... are confusing me.

我不明白...代表什么只是(整數拉鏈)我會理解有一個類的整數變量叫做拉鏈。但......讓我感到困惑。

5 个解决方案

#1

Those are "varargs," syntactic sugar that allows you to invoke the constructor in the following ways:

那些是“varargs”,語法糖,允許您通過以下方式調用構造函數:

new Weatherman()

new Weatherman(98115);

new Weatherman(98115, 98072);

new Weatherman(new Integer[0]);

Under the covers the arguments are passed to the constructor as an array, but you do not need to construct an array to invoke it.

在底層,參數作為數組傳遞給構造函數,但是您不需要構造一個數組來調用它。

#2

That’s a “vararg”. It can handle any number of Integer arguments, i.e.

這是一個“vararg”。它可以處理任意數量的Integer參數,即

new Weatherman(1);

is just as valid as

和...一樣有效

new Weatherman();

or

new Weatherman(1, 7, 12);

Within the method you access the parameters as an Integer array.

在該方法中,您可以將參數作為Integer數組進行訪問。

#3

You are seeing the varargs feature of Java, available since Java 1.5.

您將看到Java的varargs功能,從Java 1.5開始提供。

zips is an array of Integer inside the constructor, but the constructor can be called with a variable number of arguments.

zips是構造函數中的Integer數組,但可以使用可變數量的參數調用構造函數。

#4

從Java教程:

You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array).

您可以使用名為varargs的構造將任意數量的值傳遞給方法。當您不知道將多少特定類型的參數傳遞給該方法時,您可以使用varargs。這是手動創建數組的快捷方式(前一種方法可能使用了varargs而不是數組)。

To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.

要使用varargs,請使用省略號(三個點,...),一個空格和參數名稱來跟蹤最后一個參數的類型。然后可以使用任何數量的參數調用該方法,包括none。

public Polygon polygonFrom(Point... corners) {

int numberOfSides = corners.length;

double squareOfSide1, lengthOfSide1;

squareOfSide1 = (corners[1].x - corners[0].x)*(corners[1].x - corners[0].x)

+ (corners[1].y - corners[0].y)*(corners[1].y - corners[0].y) ;

lengthOfSide1 = Math.sqrt(squareOfSide1);

// more method body code follows that creates

// and returns a polygon connecting the Points

}

You can see that, inside the method, corners is treated like an array. The method can be called either with an array or with a sequence of arguments. The code in the method body will treat the parameter as an array in either case.

您可以看到,在方法內部,角被視為數組。可以使用數組或參數序列調用該方法。在任何一種情況下,方法體中的代碼都會將參數視為數組。

#5

If I remember good it is used when there is a variable number of parameters

如果我記得很好,它會在有可變數量的參數時使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值