您的第一个选择是“更严格”的参数化.这意味着,您要使用一系列限制来定义类T,然后在List中使用它.在第二种方法中,参数类T是通用的,没有条件,Lists类参数是根据参数T定义的.
第二种方式在语法上也是不同的,有一个?而不是第一个选项的T,因为在参数定义中你没有定义类型参数T而是使用它,所以第二个方法不能具体.
由此产生的实际差异是继承.你的第一个方法需要是一个类似于自身的超类的类型,而第二个类型只需要与无条件/无关的T相当:
public class Person implements Comparable {
@Override
public int compareTo(Number o) {
return 0;
}
public static > int methodX(List data) {
return 0;
}
public static int methodY(List extends Comparable super T>> data) {
return 0;
}
public static void main(String[] args) {
methodX(new ArrayList()); // stricter ==> compilation error
methodY(new ArrayList());
}
}
如果您将Person的Comparable更改为能够比较Object或Person(基类的继承树),那么methodX也将起作用.