Erasure of Generic Methods
Java编译器也会擦除泛型函数传参中的类型参数,以下面的泛型函数为例:
// Counts the number of occurrences of elem in anArray.
//
public static <T> int count(T[] anArray, T elem) {
int cnt = 0;
for (T e : anArray)
if (e.equals(elem))
++cnt;
return cnt;
}T是无边界的,所以Java编译器将它替换为Object:
public static int count(Object[] anArray, Object elem) {
int cnt = 0;
for (Object e : anArray)
if (e.equals(elem))
++cnt;
return cnt;
}假设有如下几个类的定义:
class Shape { /* ... */ }
class Circle extends Shape { /* ... */ }
class Rectangle extends Shape { /* ... */ }我们可以编译一个通用的函数来画不同的形状:
public static <T extends Shape> void draw(T shape) { /* ... */ }Java编译器会将T替换为Shape:
public static void draw(Shape shape) { /* ... */ }Last updated
Was this helpful?