标签:
Java 泛型的约束与局限性
@author ixenos
if(a instanceof Pair<String>) //ERROR,仅测试了a是否是任意类型的一个Pair,会看到编译器ERROR警告 if(a instanceof Pair<T>) //ERROR Pair<String> p = (Pair<String>) a;//WARNING,仅测试a是否是一个Pair Pair<String> stringPair = ...; Pair<Employee> employeePair = ...; if(stringPair.getClass() == employeePair.getClass()) //会得到true,因为两次调用getClass都将返回Pair.class
Pair<String> table = new Pair<String>[10]; //ERROR
Object[] objArray = table;
objArray[0] = "Hello"; //ERROR--component type is Pair
objArray[0] = new Pair<Employee>();
public static <T> Pair<T> makePair(Class<T> cl){ try{ return new Pair<>(cl.newInstance() , cl.newInstance()); } catch(Exception ex) { return null; } } //这个方法可以按照下列方式调用: Pair<String> p = Pair.makePair(String.class);
import java.lang.reflect.*; ... public static <T extends Comparable> T[] minmax(T... a){ T[] mm = (T[]) Array.newInstance(a.getClass().getComponentType() , 2); ... }
Class
。如果此类不表示数组类,则此方法返回 null。public Object[] toArray() { return Arrays.copyOf(elementData, size); }
【API文档描述】public static <T> T[] copyOf(T[] original,int newLength)
a
- 要存储列表元素的T[]数组(如果它足够大)否则分配一个具有相同运行时类型的新数组,返回该T[]数组@SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { if (a.length < size) // Make a new array of a‘s runtime type, but my contents: return (T[]) Arrays.copyOf(elementData, size, a.getClass()); //a.getClass()得运行时目的数组的运行时类型 System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; }
public static <T,U> T[] copyOf(U[] original,int newLength, Class<? extends T[]> newType)
public class Singleton<T>{ private static T singleInstance; //ERROR public static T getSingleInstance(){...} //ERROR }
public static <T extends Throwable> void doWork(Class<T> t){ try{ do work }catch (T e){ // ERROR Logger.global.info(...) } }
不过,在异常规范中使用类型变量是允许的:
public static <T extends Throwable> void doWork(T t) throws T { //此时可以throws T try{ do work }catch (Throwable realCause){ //捕获到具体实例 t.initCause(realCause); throw t; //这时候抛具体实例,所以throw t 和 throws T 是可以的! } }
@SuppressWarnings("unchecked") //SuppressWarning标注很关键,使得编译器认为T是unchecked异常从而不强迫为每一个异常提供处理器 public static <T extends Throwable> void throwAs(Throwable e) throws T{ //因为泛型和类型擦除,可以传递任意checked异常,例如RuntimeException类异常 throw (T) e; }
public abstract class Block{ public abstract void body() throws Exception; public Thread toThread(){ return new Thread(){ public void run(){ try{ body(); }catch(Throwable t){ Block.<RuntimeException>throwAs(t); } } }; } @SuppressWarnings("unchecked") public static <T extends Throwable> void throwAs(Throwable e) throws T{ throw (T) e ; } }
public class Test{ public static void main(String[] args){ new Block(){ public void body() throws Exception{ //不存在ixenos文件将产生IOException,checked异常! Scanner in = new Scanner(new File("ixenos")); while(in.hasNext()) System.out.println(in.next()); } }.toThread().start(); } }
class Calender implements Comparable<Calender>{...} class GGCalender extends Calender implements Comparable<GGCalender>{...} //ERROR
标签:
原文地址:http://www.cnblogs.com/ixenos/p/5645851.html