[英]Java wildcard in multi-level generic type
为什么Bar.go
OK
使用参数f2
而不是参数f1
?
public class HelloWorld {
public static void main(String[] args) {
Foo<Foo<?>> f1 = new Foo<Foo<?>>();
Foo<Foo<String>> f2 = new Foo<Foo<String>>();
Bar.go(f1); // not OK
Bar.go(f2); // OK
}
public static void p(Object o) {
System.out.println(o);
}
}
class Foo<E> {
}
class Bar {
public static <T> void go(Foo<Foo<T>> f) {
}
}
不应该编译器自动推断类型T
为capture of ?
在这两种情况下?
Foo<Foo<?>> f1 = new Foo<Foo<?>>();
这意味着类型是未知的,任何类型的对象都可以添加到异构的Foo<Foo<?>>
,编译器不能保证Foo<Foo<?>>
中的所有对象都是相同的类型。 因此,它无法传递给采用有界类型作为参数的Bar.go
你可以改为声明为Foo<Foo<Object>> f1 = new Foo<Foo<Object>>();
将它传递给Bar.go
,你明确提到一切都是Object
类型。
好问题!
(在下面的评论,WRT一类通用的在E
等Foo< E >
中,定义“协变方法”作为返回的方法E
不使用具有任何参数E
,以及“逆变方法”为相反:一个这需要类型E
的形式参数,但不返回涉及E
的类型。[这些术语的真正定义更复杂,但现在不记得了。])
在f1
的情况下,似乎编译器试图将T
绑定到Object
,因为如果你这样做的话
class Bar0 {
public static < T > void go( Foo< Foo< ? extends T > > f ) {
// can pass a Foo< T > to a contravariant method of f;
// can use any result r of any covariant method of f,
// but can't pass T to any contravariant method of r
}
}
然后go(f1)
工作,但现在go(f2)
没有,因为即使Foo< String > <: Foo< ? extends String >
Foo< String > <: Foo< ? extends String >
,这并不意味着Foo< Foo< String > > <: Foo< Foo< ? extends String > >
Foo< Foo< String > > <: Foo< Foo< ? extends String > >
。
以下是为f1
和f2
编译的一些修改:
class Bar1 {
public static < T > void go( Foo< ? super Foo< T > > f ) {
// can't properly type the results of any covariant method of f,
// but we can pass a Foo< T > to any contravariant method of f
}
}
class Bar2 {
public static < T > void go( Foo< ? extends Foo< ? extends T > > f ) {
// can't pass a Foo< T > to a contravariant method of f;
// can use result r of any covariant method of f;
// can't pass a T to a contravariant method of r;
// can use result of covariant method of r
}
}
一个很好的阅读多级通配符是什么意思?
例:
Collection< Pair<String,Long> > c1 = new ArrayList<Pair<String,Long>>();
Collection< Pair<String,Long> > c2 = c1; // fine
Collection< Pair<String,?> > c3 = c1; // error
Collection< ? extends Pair<String,?> > c4 = c1; // fine
当然,我们可以将Collection<Pair<String,Long>>
分配给Collection<Pair<String,Long>>
。 这里没有什么可惊讶的。
但是我们不能将Collection<Pair<String,Long>>
分配给Collection<Pair<String,?>>
。 参数化类型Collection<Pair<String,Long>>
是String和Long对的同类集合; 参数化类型Collection<Pair<String,?>>
是一组String和一些未知类型的异构集合。 异构Collection<Pair<String,?>>
可以包含一个Pair<String,Date>
,它显然不属于Collection<Pair<String,Long>>
。 因此,不允许分配。
我将证明,如果编译器允许Bar.go(f1);
,类型系统(安全)将被打破:
Java语法允许您使用T
作为类型来在go()
声明变量。 类似的东西: T t = <something>
。
现在,让我们使用ArrayList
而不是Foo
,
然后我们有:
class HW {
public static void main(String[] args) {
ArrayList<ArrayList<?>> f1 = new ArrayList<ArrayList<?>>();
go(f1); // not OK
}
public static <T> void go(ArrayList<ArrayList<T>> f) {
}
}
ArrayList<?>
是ArrayList<String>
超类型,它也是ArrayList<Integer>
超类型,这意味着您可以在main
执行以下操作:
ArrayList<?> s = new ArrayList<String>();
f1.add(s);
ArrayList<?> i = new ArrayList<Integer>();
f1.add(i);
现在,让我们假设编译器允许您使用f1
作为参数调用go()
。 推断T
的选项是:
T = Object
,但ArrayList<ArrayList<Object>>
不是ArrayList<ArrayList<?>>
因为ArrayList<Object>
与ArrayList<?>
类型不同,所以不允许选项。
T = ?
,那么我们就能做到:
public static <T> void go(ArrayList<ArrayList<T>> f) { ArrayList<T> alt1 = f.get(0); // ArrayList<String> T str = alt1.get(0); ArrayList<T> alt2 = f.get(1); // ArrayList<Integer> alt2.add(str); // We have added String to List<Integer> // ... type system broken }
go()
在你必须做的两种情况下工作:
public static void go(ArrayList<? extends ArrayList<?>> f) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.