看看这个 Java 泛型的简单例子:
class List<T> {
T head;
List<T> next;
}
class A<T> {
List<T> l;
public <T> int length() {
List<T> l = this.l;
int c = 1;
while (l.next != null) {
c++;
l = l.next;
}
return c;
}
public static void main(String[] args) {
A<Integer> a = new A<Integer>();
a.l = new List<Integer>();
a.l.head = 123;
a.l.next = new List<Integer>();
a.l.next.head = 432;
System.out.println("list length: " + a.length());
}
}
它得到一个编译错误,声称类型不兼容,但声称两个变量是同一类型:
$ javac A.java && java A
A.java:10: incompatible types
found : List<T>
required: List<T>
List<T> l = this.l;
^
1 error
如果我将 length() 的第一行更改为 List<T> l = (List<T>)(Object)this.l;
, 有用。为什么?
最佳答案
您已使用此行在泛型类中声明了一个泛型方法:
public <T> int length() {
这<T>
与您类(class)的 <T>
不同.根据JLS Section 6.3 :
The scope of a class's type parameter (§8.1.2) is the type parameter section of the class declaration, the type parameter section of any superclass or superinterface of the class declaration, and the class body.
您无需重新声明 <T>
在你的方法上;该类的类型参数已在范围内。
使用类的通用类型参数 <T>
, 让你的方法不声明另一个 <T>
并简单地使用你类(class)的<T>
:
public int length() {
要了解您的转换为何有效:
List<T> l = (List<T>)(Object)this.l;
您可以将任何对象转换为 Object
.然后将结果转换为 List<T>
.您可以随时将其转换到任何您想要的东西; Java 只会抛出 ClassCastException
。在运行时如果它不是真正的 List
在运行时。但是编译器也会给出一个警告,它使用了未经检查或不安全的操作,因为它不能保证这个 <T>
是原来的<T>
.
为了说明<T>
之间的区别s,你可以使用<U>
作为方法的通用类型参数并获得相同的结果:
public <U> int length() {
List<U> l = (List<U>)(Object)this.l;
这与相同类型的安全警告一起编译。
如果你真的知道可以保证类型安全,你可以使用@SuppressWarnings("unchecked")
注释你的方法。但在这里,我仍然会从方法中完全删除泛型类型参数,并使用类的类型参数。
关于java - List<T> 不等于 List<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282484/