java基础——泛型
泛型类
有一个类型参数声明部分(由尖括号分隔),该类型参数声明部分在方法返回类型之前(在下面例子中的<E>)
public static <E extends Comparable<E>> void getMax(E x, E y, E z) { E max = x; if (y.compareTo(max) > 0) { max = y; } if (z.compareTo(max) > 0) { max = z; } }
public static <T> T printArray(T[] array) { T t = array[0]; System.out.println(t); return t; }
类型通配符:
public static void getData(List<? extends Number> data) { System.out.println(data.get(0)); }
有界的类型参数:
"extends"——"extends"(类)或者"implements"(接口),上界
“super ”——下界, List<? super Number>来定义,表示类型只能接受Number及其三层父类类型,如 Object 类型的实例。

浙公网安备 33010602011771号