泛型是java三大高級特性之一,利用JVM的解釋器功能,能夠讓咱們的代碼應用於多種類型的處理。其核心思想是在newInstance()實例的時候,告訴編譯器想使用什麼類型,而後編譯器幫你處理一切細節。大量框架都是利用了泛型來寫通用的處理邏輯,下面簡單介紹泛型接口和方法上。java
編譯器對泛的型處理,java會使將具體的類型信息都擦出啊,你惟一知道的是在使用一個對象。當加入了邊界信息<T extends 類>,編譯器在編譯的時候會將擦出掉類信息,而後將其編譯爲邊界類。
數組
a)泛型至關於一個佔位符,編譯器自動幫你轉化爲一個 java.lang.object 的根類。
框架
b)泛型邊界能夠讓編譯將泛型復活轉換惟一個邊界類型。
ide
c)爲了在泛型類經過反射中操做具體類的各類數據,咱們能夠使用class<T> t 來建立咱們一個實例。這樣就能夠經過泛型來寫通用的類處理框架。
this
//接口 // T 至關於一個佔位符,在編譯器處理時候明確類型 public interface Generator<T> { // 在create的時候,編譯器知道是哪個類 T next(); // 正確泛型方法,在使用這個方法的時候,編譯器會從參數組中查詢須要的類型 <S> S create(Class<S> resultObject); // 錯誤泛型方法、在編譯器並不知道M這個東西是什麼 // M over(); } // 類 public class TypeParam<T> implements Generator<T> { private T t1; @Override public T next() { return null; } @Override public <S> S create(Class<S> resultObject) { return null; } public T getT1() { return t1; } public void setT1(T t1) { this.t1 = t1; } public static void main(String[] args) { } } 使用 javap -c TypeParam.class Compiled from "TypeParam.java" public class typeParam.TypeParam<T> implements typeParam.Generator<T> { public typeParam.TypeParam(); Code: 0: aload_0 1: invokespecial #10 // Method java/lang/Object."<init>":()V 4: return public T next(); Code: 0: aconst_null 1: areturn public <S> S create(java.lang.Class<S>); //邊界補償,使用(class<?> xxx )能夠明確的在泛型方法中使用反射 Code: 0: aconst_null 1: areturn public static void main(java.lang.String[]); Code: 0: return } Compiled from "TypeParam.java" public class typeParam.TypeParam<T> implements typeParam.Generator<T> { public typeParam.TypeParam(); Code: 0: aload_0 1: invokespecial #14 // Method java/lang/Object."<init>":()V 4: return public T next(); Code: 0: aconst_null 1: areturn public <S> S create(java.lang.Class<S>); Code: 0: aconst_null 1: areturn public T getT1(); Code: 0: aload_0 1: getfield #32 // Field t1:Ljava/lang/Object; //無邊界,將其編譯爲一個object類型 4: areturn public void setT1(T); Code: 0: aload_0 1: aload_1 2: putfield #32 // Field t1:Ljava/lang/Object; //無邊界,將其編譯爲一個object類型 5: return public static void main(java.lang.String[]); Code: 0: return }
public class TypeExtends<Q extends String> { private Q q; Q delete() { return q; } public Q getQ() { return q; } public void setQ(Q q) { this.q = q; } } 使用 javap -c TypeExtends.class Compiled from "TypeExtends.java" public class typeParam.TypeExtends<Q extends java.lang.String> { public typeParam.TypeExtends(); Code: 0: aload_0 1: invokespecial #12 // Method java/lang/Object."<init>":()V 4: return Q delete(); Code: 0: aload_0 1: getfield #23 // Field q:Ljava/lang/String; //有邊界,將其編譯爲邊界類型 4: areturn } Compiled from "TypeExtends.java" public class typeParam.TypeExtends<Q extends java.lang.String> { public typeParam.TypeExtends(); Code: 0: aload_0 1: invokespecial #12 // Method java/lang/Object."<init>":()V 4: return Q delete(); Code: 0: aload_0 1: getfield #23 // Field q:Ljava/lang/String; //有邊界,將其編譯爲邊界類型 4: areturn public Q getQ(); Code: 0: aload_0 1: getfield #23 // Field q:Ljava/lang/String; //有邊界,將其編譯爲邊界類型 4: areturn public void setQ(Q); Code: 0: aload_0 1: aload_1 2: putfield #23 // Field q:Ljava/lang/String; //有邊界,將其編譯爲邊界類型 5: return }
public class compansate<T> { Class<T> mytypes; public T getType() throws InstantiationException, IllegalAccessException { return mytypes.newInstance(); } @Test public <S> S getType(Class<S> args) throws InstantiationException, IllegalAccessException { return args.newInstance(); } } 使用 javap -c compansata.class Compiled from "compansate.java" public class typeParam.compansate<T> { java.lang.Class<T> mytypes; //經過class來處理類信息 public typeParam.compansate(); Code: 0: aload_0 1: invokespecial #12 // Method java/lang/Object."<init>":()V 4: return public T getType() throws java.lang.InstantiationException, java.lang.IllegalAccessException; Code: 0: aload_0 1: getfield #28 // Field mytypes:Ljava/lang/Class; 4: invokevirtual #30 // Method java/lang/Class.newInstance:()Ljava/lang/Object; 7: areturn public <S> S getType(java.lang.Class<S>) throws java.lang.InstantiationException, java.lang.IllegalAccessException; Code: 0: aload_1 1: invokevirtual #30 // Method java/lang/Class.newInstance:()Ljava/lang/Object; 4: areturn }