【发布时间】:2016-03-03 02:40:48
【问题描述】:
我们来看下面的例子:
public class Test {
public void main(String[] args) {
int[] someInts = {1, 2, 5};
new Dummy(1, someInts, "Hello"); //works
new Dummy(1, new int[] {1, 2, 5}, "Hello"); //works
new Dummy(1, {1, 2, 5}, "Hello"); //fails
new Dummy(1, [1, 2, 5], "Hello"); //fails
}
public class Dummy {
Dummy(int someNumber, int[] someArray, String message) {
}
}
}
对于两个失败的行,Eclipse 说:“构造函数 Test.Dummy(int, int, int, int, String) 是未定义的”
首先,我不明白为什么它不将数组识别为数组(仅在失败的行中)。
其次,为什么不能直接将数组传入构造函数,而必须创建一个变量来传递呢?
第三,有没有一种方法可以创建一个构造函数,该构造函数采用类似该行的内容,即没有变量或new int[] {...} 语句?
如果有人知道在标题中表达这一点的更好方法,请随时改进它。
【问题讨论】:
标签: java arrays constructor arguments