This code not compiles, because of 'A' expression. It's interesting thing: in A expression expected
List<Foo>
generic type, but got List<anonymous Foo>
(according compiler). Is it a jdk bug or feature?
接口 Foo{ void doFoo(); }
public class GenericsTest {
public static<V> List<V> bar(V v){ return new ArrayList<V>();}
public static void main(String[] args) {
// 下面这一行代码无法编译,因为 Foo 类型不是一个参数类型
List<Foo> f = bar(new Foo(){ //A
public void doFoo() { }
});
Foo fooImpl = new Foo(){
public void doFoo() { }
};
// 下面这一行代码可以编译,因为 fooImpl 是 Foo 的实现类
List<Foo> f2 = bar(fooImpl);
}
}
Optional<T>
,也用于Optional.<Foo>absent()
。 - Maarten Bodewes