【发布时间】:2015-03-21 03:22:13
【问题描述】:
建议什么时候做:
public <E> boolean hasPropertyX(List<E extends User> alist);
对
public boolean hasPropertyX(List<? extends User> alist);
看起来它们都可以正常工作。
【问题讨论】:
-
This reflects the fact that the type parameter is not being used to express any kind of interdependency between the type(s) of the argument(s), the return type and/or throws type. In the absence of such interdependency, generic methods are considered bad style, and wildcards are preferred.Java 语言规范 8 §4.5.1-2。基本上,如果对方法之外的类型存在依赖关系,则应使用第一个。否则,使用第二个。