我有这个代码:
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.callTest();
}
public void callTest() {
GenericTest gt = new GenericTest(); // this line don't compile
gt.test(new Date());
}
class GenericTest {
T x;
public void test(T y) {
System.out.println(x.toString());
}
}
}
我明白为什么新的GenericTest< Date>()不编译,这是因为Date不实现List接口,但是如果我实例化GenericTest gt = new GenericTest()而不具有泛型,整个代码可以工作,我不明白为什么方法测试期望(T y)其中T扩展Date并实现List,但它与gt.test(new Date())一起使用.