Collections.emptyList()返回List ?

Collections.emptyList()返回List ?,第1张

Collections.emptyList()返回List ?

您遇到的问题是,即使该方法

emptyList()
返回了
List<T>
,您也没有为其提供类型,因此它默认为
returning List<Object>
。您可以提供type参数,并使代码的行为符合预期,如下所示:

public Person(String name) {  this(name,Collections.<String>emptyList());}

现在,当您进行直接分配时,编译器可以为您找出通用类型参数。这称为类型推断。例如,如果您这样做:

public Person(String name) {  List<String> emptyList = Collections.emptyList();  this(name, emptyList);}

那么该

emptyList()
呼叫将正确返回
List<String>



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5565334.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存