标签:xtend getc win out row exce 泛型类 com each
/**
* 泛型类声明的泛型参数占主导地位,如泛型类声明的参数为<T>,则泛型接口的泛型参数也应为<T>;
* 当泛型类声明的参数为<T>时,泛型接口的参数<T>可以不写;
* @param <T>
*/
public class LinkedList<T> implements Iterable {
@Override
public Iterator iterator() {
return null;
}
@Override
public void forEach(Consumer action) {
}
@Override
public Spliterator spliterator() {
return null;
}
}
public class GenericMethods{
public <T> void f(T x) {
System.out.println(x.getClass().getName());
}
}
在泛型代码内部,无法获得任何与关泛型参数类型的信息
List
泛型类型参数将擦除到它的第一个边界
之所以使用擦除来实现泛型,是为了迁移兼容性
class ClassAsFactory<T>{
T x;
public ClassAsFactory(Class<T> kind){
try {
x = kind.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public class ZwbHeap<T extends Comparable<? super T>> {
private T[] nodes;
private int size;
private int capacity = 16;
public ZwbHeap() {
// 这样转换会失败
//nodes = (T[])new Object[capacity+1];
// 必须这样写
nodes = (T[])new Comparable[capacity+1];
}
}
标签:xtend getc win out row exce 泛型类 com each
原文地址:https://www.cnblogs.com/textworld/p/12111204.html