13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

基础篇:深入解析Java泛型

本文转载自微信公众号「潜行前行」,作者cscw 。转载本文请联系潜行前行公众号。

创新互联建站专业为企业提供芦山网站建设、芦山做网站、芦山网站设计、芦山网站制作等企业网站建设、网页设计与制作、芦山企业网站模板建站服务,10年芦山做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

 1 JAVA的Type类型体系

先了解下java的Type类型体系(类的类=>类型),Type是所有类型(原生类型-Class、参数化类型-Parameterizedtype、数组类型-GenericArrayType、类型变量-TypeVariable、基本类型-Class)的共同接口;前两篇反射和注解讲到的Class 就是Type的一实现类

2 泛型的概念

泛型: 把类型明确的工作推迟到创建对象或调用方法的时候才去明确的特殊的类型

3 泛型类和泛型方法的示例

 
 
 
 
  1. public class MainTest { 
  2.     private  T param; 
  3. public static void main(String[] args){ 
  4.         MainTest data = new MainTest(){}; 
  5.         ParameterizedType genType1 = (ParameterizedType)data.getClass().getGenericSuperclass(); 
  6.   } 
 
 
 
 
  1. public class MainTest{ 
  2.     public static void main(String[] args){ 
  3.         printData("siting"); 
  4.     } 
  5.     static   T printData(T t){ 
  6.         System.out.println(t); 
  7.         return t; 
  8.     } 

接口和抽象类都可以使用泛型

4 类型擦除

 
 
 
 
  1. public class MainTest { 
  2.     public static void main(String[] args){ 
  3.         List strArr  = new ArrayList<>(); 
  4.         List intArr  = new ArrayList<>(); 
  5.         Type strClazz = strArr.getClass(); 
  6.         Type intClazz = intArr.getClass(); 
  7.     } 

 
 
 
 
  1. public class MainTest { 
  2.     T param; 
  3.     public static void main(String[] args){ 
  4.         MainTest test = new MainTest<>(); 
  5.         test.setParam("siting"); 
  6.     } 
  7.     public T getParam() {  return param;   } 
  8.     public void setParam(T param) {  this.param = param;  } 
  9. public class com/MainTest { 
  10.   ...省略 
  11.   public static main([Ljava/lang/String;)V 
  12.    L0 
  13.     LINENUMBER 7 L0 
  14.     NEW com/MainTest 
  15.     DUP 
  16.     INVOKESPECIAL com/MainTest. ()V 
  17.     ASTORE 1 
  18.    L1 
  19.     LINENUMBER 8 L1 
  20.     ALOAD 1 
  21.     LDC "siting"     // 调用类型擦除后的setParam(Object) 
  22.     INVOKEVIRTUAL com/MainTest.setParam (Ljava/lang/Object;)V 
  23.    L2 
  24.    ...省略//getParam 的返回值是Object 
  25.   public getParam()Ljava/lang/Object; 
  26.    L0 
  27.     LINENUMBER 10 L0 
  28.     ALOAD 0 
  29.     GETFIELD com/MainTest.param : Ljava/lang/Object; 
  30.     ARETURN 
  31.    ...省略//setParam 的入参是Object 
  32.   public setParam(Ljava/lang/Object;)V 
  33.    L0 
  34.     LINENUMBER 11 L0 
  35.     ALOAD 0 
  36.     ALOAD 1 
  37.     PUTFIELD com/MainTest.param : Ljava/lang/Object; 
  38.     RETURN 
  39.    ... 

可以看出T(String)都被转换为Object类型,最初的初始化的String不见了

5 泛型的继承

 
 
 
 
  1. public class MainTest { 
  2.     T param; 
  3.     static public class SubTest1 extends MainTest{} 
  4.     static public class SubTest2 extends MainTest{} 
  5.     //SubTest3继承的时原生类型 
  6.     static public class SubTest3 extends MainTest{} 

6 泛型变量TypeVariable

 
 
 
 
  1. public class MainTest { 
  2.     List param; 
  3.     public static void main(String[] args) throws Exception{ 
  4.         Class clazz =  MainTest.class; 
  5.         TypeVariable[] typeVariable = clazz.getTypeParameters(); 
  6.         // 1 
  7.         Field field = clazz.getDeclaredField("param"); 
  8.         ParameterizedType arrayType = (ParameterizedType)field.getGenericType(); 
  9.         // interface List 的泛型类型E被T,具体化,因此其被识别为 TypeVariable 
  10.         TypeVariable variable1 = (TypeVariable)arrayType.getActualTypeArguments()[0]; 
  11.         // 2 
  12.         ParameterizedType type = (ParameterizedType)SubTest.class.getGenericSuperclass(); 
  13.         TypeVariable variable2 = (TypeVariable)type.getActualTypeArguments()[0]; 
  14.     } 
  15.     static class SubTest extends MainTest{} 

7 参数化类型ParameterizedType

 
 
 
 
  1. public interface ParameterizedType extends Type { 
  2.     //获取实际参数,List里的String; 如果是List则是TypeVariable类型 
  3.     Type[] getActualTypeArguments();  
  4.     // 获取原始类型List -> List 
  5.     Type getRawType();   
  6.     Type getOwnerType(); 
 
 
 
 
  1. public class MainTest { 
  2.     public static void main(String[] args){ 
  3.         MainTest str = new MainTest(); 
  4.         Class variable = str.getClass(); 
  5.         Type genType1 = variable.getGenericSuperclass(); 
  6.     } 

 
 
 
 
  1. // 1 子类继承泛型时,指定具体参数(可以是String等已知类型,也可以是子类的泛型参数) 
  2. // 2 获取在类内部定义的泛型属性,需指定具体泛型参数 
  3. // 3 局部代码,可以通过泛型的匿名内部子类(需指定具体泛型参数)获取ParameterizedType类型 
  4. public class MainTest { 
  5.     List list; 
  6.     public static void main(String[] args) throws NoSuchFieldException { 
  7.         SubTest str = new SubTest<>(); 
  8.         // 方式一 
  9.         Class variable = str.getClass(); 
  10.         // 父类是(521)ParameterizedType类型 
  11.         ParameterizedType genType = (ParameterizedType)variable.getGenericSuperclass(); 
  12.         // (521)ParameterizedType类型的原生类型是(479)class com.MainTest 
  13.         Type clazz = genType.getRawType(); 
  14.         //MainTest.class 的原生类型是 (479)class com.MainTest 
  15.         Class rawClazz = MainTest.class; 
  16.  
  17.         //方式二,泛型属性 
  18.         Field field = rawClazz.getDeclaredField("list"); 
  19.         //属性list 类型是(546)ParameterizedType类型List 
  20.         ParameterizedType fieldType = (ParameterizedType)field.getGenericType(); 
  21.  
  22.         // 方式三 
  23.         MainTest sub3 = new MainTest(){}; 
  24.         // clazz3是匿名子类 
  25.         Class clazz3 =  sub3.getClass(); 
  26.         //父类是(555)ParameterizedType类型 
  27.         ParameterizedType genType3 = (ParameterizedType) clazz3.getGenericSuperclass(); 
  28.         // (555)ParameterizedType类型的原生类型是(479)class com.MainTest 
  29.         Type type3 = genType3.getRawType(); 
  30.     } 
  31.     public static class SubTest extends MainTest{ } 

8 通配符(WildcardType)

无边界通配符:无界通配符 ? 可以适配任何引用类型:

 
 
 
 
  1. public static void print(List list){}  
  2. ----->>> 
  3. public static void print(List list){}  

上界限定通配符 < ? extends E>

下界限定通配符 < ? super E>

 
 
 
 
  1. class Parent{ } 
  2. class Child extends Parent{ } 
  3. public class MainTest { 
  4.     T param; 
  5.     public static void main(String[] args){ 
  6.         MainTest parent_m = new MainTest<>(); 
  7.         parent_m.setParam(new Child()); 
  8.         Object parent = parent_m.getParam(); 
  9.     } 
  10.     public T getParam() {  return param;  } 
  11.     public void setParam(T param) {  this.param = param; } 

9 泛型数组(GenericArrayType)

 
 
 
 
  1. public interface GenericArrayType extends Type { 
  2.     //获得这个数组元素类型,即获得:A(A[])或  T(T[]) 
  3.     Type getGenericComponentType(); 
 
 
 
 
  1. public class MainTest { 
  2.     T[] param; 
  3.     public static void main(String[] args) throws Exception{ 
  4.         Class clazz =  MainTest.class; 
  5.         Field field = clazz.getDeclaredField("param"); 
  6.         GenericArrayType arrayType = (GenericArrayType)field.getGenericType(); 
  7.         TypeVariable variable = (TypeVariable) arrayType.getGenericComponentType(); 
  8.     } 

本文名称:基础篇:深入解析Java泛型
URL标题:http://cdbrznjsb.com/article/cccciig.html

其他资讯

让你的专属顾问为你服务