使用通配符的Java自引用泛型
作者:互联网
是否可以指定未知泛型类型是自引用的?
尝试失败:
import java.util.*;
class Generics {
public enum A { A1, A2 }
public enum B { B1, B2 }
public static List<? extends Enum<?>> listFactory(String[] args) {
if (args.length == 0) {
return new ArrayList<A>(Arrays.asList(A.A1, A.A2));
} else {
return new ArrayList<B>(Arrays.asList(B.B1, B.B2));
}
}
public static void main(String[] args) {
List<? extends Enum<?>> lst = listFactory(args);
dblList(lst);
System.out.println(lst);
}
public static <EType extends Enum<EType>> void dblList(List<EType> lst) {
int size = lst.size();
for (int i = 0; i < size; i++) {
lst.add(lst.get(i));
}
}
}
这会导致编译错误:
Generics.java:17: error: method dblList in class Generics cannot be applied to given types;
dblList(lst);
^
required: List<EType>
found: List<CAP#1>
reason: inferred type does not conform to declared bound(s)
inferred: CAP#1
bound(s): Enum<CAP#1>
where EType is a type-variable:
EType extends Enum<EType> declared in method <EType>dblList(List<EType>)
where CAP#1 is a fresh type-variable:
CAP#1 extends Enum<?> from capture of ? extends Enum<?>
1 error
理想情况下,listFactory()的返回类型将表示该列表包含自引用泛型类型(其确切类型未知).
这可能吗?如果是这样,listFactory()和lst的类型应该是什么?
解决方法:
有效的Java Item 28不鼓励在返回类型中使用通配符:
Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code.
Properly used, wildcard types are nearly invisible to users of a class. They cause methods to accept the parameters they should accept and reject those they should reject. If the user of a class has to think about wildcard types, there is probably something wrong with the class’s API.
这是EJ描述的问题的一个很好的例子. listFactory()实际上只是返回一个List< Enum<?>>但是通过声明一个通配符返回类型,你必须通过箍来完成看似简单的任务.
如果你给listFactory()一个像这样的签名:
public static List<Enum<?>> listFactory(String[] args)
您也可以清理dblList()的签名:
public static <E> void dblList(List<E> lst)
标签:java,wildcard,generics,self-reference 来源: https://codeday.me/bug/20190706/1398637.html