对于通用接口:
public interface Foo<T> {
void f(T t);
}
Run Code Online (Sandbox Code Playgroud)
两个字段之间的区别:
public class Bar {
Foo foo1;
Foo<?> foo2;
}
Run Code Online (Sandbox Code Playgroud)
这foo2是通用类型而foo不是.因为?是一个通配符(我认为这意味着任何类型),并且每个类型都是Object的子类型,所以我期望Foo<?>并Foo<Object>在语义和语法上等同.
但是,请查看以下内容:
public class Puzzler {
void f() {
Integer i = null;
Foo<?> foo1 = null;
foo1.foo(i); // ERROR
Foo foo2 = null;
foo2.foo(i); // OKAY
Foo<Integer> foo3 = null;
foo3.foo(i); // OKAY
Foo<Object> foo4 = null;
foo4.foo(i); // OKAY
}
private interface Foo<T> {
void foo(T t);
}
} …Run Code Online (Sandbox Code Playgroud)