参见英文答案 >
What is a raw type and why shouldn’t we use it? 14个
给定一个通用接口:
public interface I {
public int interfaceMethod(E s);
}
以及实现接口的泛型类
public class A implements I {
private T val;
public A(T x) {
val = x;
}
public int interfaceMethod(T val) {
// looks like T should be of the same type as instance variable 'val'
return 0;
}
}
为什么以下工作?
public class Run {
public static void main(String[] args) {
A a = new A("hello");
System.out.println(a.interfaceMethod(100)); \\ returns 0
}
}
我期望在类A中定义的方法interfaceMethod的T type参数将方法约束为与提供给A的构造函数的类型相同的参数(在本例中为String).
为什么a.interfaceMethod不需要String类型的参数?