Java泛型的声明方式
Java泛型的声明方式有两种:
- 类型参数声明:在类,接口或方法的声明中,使用尖括号<>来声明一个或多个类型参数。例如:
public class MyGenericClass<T> {
// 类体
}
public interface MyGenericInterface<K, V> {
// 接口体
}
public <T> void myGenericMethod(T param) {
// 方法体
}
- 使用已有的泛型类型:在使用已有的泛型类型时,可以在尖括号<>中指定具体的类型参数。例如:
List<String> stringList = new ArrayList<String>();
Map<String, Integer> stringIntegerMap = new HashMap<String, Integer>();
在这种情况下,List和Map是已有的泛型类型,String和Integer是具体的类型参数。
原文地址: https://www.cveoy.top/t/topic/jbJ6 著作权归作者所有。请勿转载和采集!