Java:一个构造函数或方法将接受数组或集合或列表或...?
在Java中是否有一个构造函数可以接受数组或集合?我已经摆弄这个有一段时间了,但我认为这是不可能的。
我希望能够初始化 MyClass
,如下所示:
MyClass c = new MyClass({"rat", "Dog", "Cat"});
如下所示:
LinkedList <String> l = new <String> LinkedList();
l.add("dog");
l.add("cat");
l.add("rat");
MyClass c = new MyClass(l);
这就是 MyClass 的样子。我可以将 XXX 设置为什么才能使其正常工作?我知道我可以重载构造函数,但如果我可以最小化代码那就太棒了,对吧?
public class MyClass{
private LinkedHashSet <String> myList;
public MyClass(XXX <String> input){
myList = new LinkedHashSet <String> ();
for(String s : input){
myList.put(s);
}
}
}
In Java is there anyway to have one constructor that will accept an array or a collection? I have been fiddling with this for a while but I don't think it is possible.
I would like to be able to initialize MyClass
, like this:
MyClass c = new MyClass({"rat", "Dog", "Cat"});
And like this:
LinkedList <String> l = new <String> LinkedList();
l.add("dog");
l.add("cat");
l.add("rat");
MyClass c = new MyClass(l);
This is what MyClass looks like. What can I make XXX be so that this will work? I know that I could overload the constructor, but if I can minimize code that would be awesome right?
public class MyClass{
private LinkedHashSet <String> myList;
public MyClass(XXX <String> input){
myList = new LinkedHashSet <String> ();
for(String s : input){
myList.put(s);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以声明两个构造函数并从第一个构造函数调用第二个构造函数:
调用看起来像
由于第一个构造函数中只有一行代码,因此它非常简约。
You can declare two constructors and call second from first:
Calls would look like
Since there's only one line of code in the first constructor, it's pretty minimalistic.
公共类 MyClass1 {
或
public class MyClass1 {
or
看来数组是不可迭代的,所以没有办法做到这一点。不可能有一个接受数组和其他可迭代对象的构造函数。这特别烦人,因为我们可以这样做:
请参阅这篇文章了解更多详细信息:为什么数组不能分配给 Iterable?
It appears that arrays are not iterable so there is no way to do this. It is impossible to have one constructor that accepts arrays AND other iterables. This is especially annoying since we can do this:
See this post for more details: Why is an array not assignable to Iterable?
理论上,您可以像这样声明构造函数:
但在我看来,这将是一个糟糕的 API 设计,因为它将所有类型检查都转移到运行时。 (没有其他方法可以使用单个构造函数来完成此操作。数组类型和
Collection
接口的唯一公共超类型是Object
。)使用构造函数要好得多如其他答案中所述的重载。
顺便说一句,以下内容(来自您的示例)是 Java 语法错误,无论构造函数声明的参数类型如何:
这种形式的数组初始值设定项只能在变量声明中使用;例如
或作为数组创建表达式的一部分;例如
In theory, you could declare the constructor something like this:
but IMO, that would be a bad API design as it punts all type checking to runtime. (There is no other way to do this with a single constructor. The only common supertype of array types and the
Collection
interface isObject
.)It is far better to use constructor overloading as described in the other answers.
Incidentally, the following (from your example) is a Java syntax error, irrespective of the constructor's declared argument types:
That form of array initializer can only be used in a variable declaration; e.g.
or as part of an array creation expression; e.g.
如果你确实想要一个构造函数,你可以使用 Arrays.asList ,然后让构造函数采用一个包含 List 和 Set 的 Collection 。
我个人会有两个构造函数。
If you really want a single constructor you could use Arrays.asList and then have the constructor take a Collection which covers List and Set.
I personally would have two constructors.