- Java泛型引起的问题:
- 桥接方法
- 不支持泛型数组
- 无法在泛型类或泛型方法中使用new T、new T[]、instanceOf
- 泛型类型变量不能是基本类型
- 无法像C++一样提供实例化声明、模版特例化、模版重载、引用折叠
package com.example.lib;
import java.util.ArrayList;
import java.util.Comparator;
public class GenericTest {
public static void main(String[] args) {
Integer[] ia = {1, 2, 3};
Integer[] ret = merge(ia);
GenericTest2<String> genericTest2 = new GenericTest2();
genericTest2.setT("hello");
String s = genericTest2.getT();
}
public static <T> ArrayList<? extends Comparable<T>> merge(ArrayList<? extends Comparable<T>> a,
ArrayList<? extends Comparable<T>> b) {
return null;
}
public static <T> ArrayList<T> merge(ArrayList<T> a, ArrayList<T> b, Comparator<T> comparator, ArrayList<T>[] arrayLists) {
new ArrayList<T>(3);
return null;
}
public static <T> void merge(T[] a, T[] b, T[] ret, Comparator<T> comparator) {
ret[0] = a[0];
T[] tl = (T[]) new Object[10];
}
public static <T> T[] merge(T[] a) {
Object[] obj = new Object[]{2, 4, 6};
return (T[]) obj;
}
}
class GenericTest2<T> {
T t;
public void setT(T t) {
this.t = t;
}
public T getT() {
return t;
}
}