public class GenricDefine {
public static void main(String[] args) {
min(2, 5);
Number ret1 = min(21.3, 4); // 类型推断取类型的交集
Object ret2 = min(2, "abc"); // 类型推断取类型的交集
Integer[] arr = {2,3,4, 6,7,9};
swap(arr, 2, 5);
// int[] arr2 = {2,3,4, 6,7,9};
// swap(arr2, 2, 5); <T>只有引用类型才能作为范型方法的实际参数。
for (Integer integer : arr) {
System.out.print(integer);
}
}
public static <T> T min(T x, T y) {
return null;
}
public static <T> void swap(T[] arr, int pos, int pos2) {
T temp = arr[pos];
arr[pos] = arr[pos2];
arr[pos2] = temp;
}
}
自定义范型类型
最新推荐文章于 2025-04-08 16:22:13 发布