zoukankan      html  css  js  c++  java
  • ParameterizedType的作用

    public interface ParameterizedType  extends Type


    subParam.Java

    1. package com.example.test;  
    2.   
    3. public class subParam extends param<myclass, myinvoke> {  
    4.     public static void main(String[] args) throws Exception{  
    5.         subParam  s = new subParam();  
    6.     }  
    7. }  

    param.java

    1. package com.example.test;  
    2.   
    3. import java.lang.reflect.ParameterizedType;  
    4. import java.lang.reflect.Type;  
    5.   
    6. public class param<T1, T2> {  
    7.     class A {}  
    8.     class B extends A {}  
    9.       
    10.     private Class<T1> entityClass;  
    11.     protected param (){  
    12.         Type type = getClass().getGenericSuperclass();  
    13.         System.out.println("getClass()==" + getClass());  
    14.         System.out.println("type = " + type);  
    15.         Type trueType = ((ParameterizedType)type).getActualTypeArguments()[0];  
    16.         System.out.println("trueType1 = " + trueType);  
    17.         trueType = ((ParameterizedType)type).getActualTypeArguments()[1];  
    18.         System.out.println("trueType2 = " + trueType);  
    19.         this.entityClass = (Class<T1>)trueType;  
    20.           
    21.           
    22.         B t = new B();  
    23.         type = t.getClass().getGenericSuperclass();  
    24.       
    25.         System.out.println("B is A's super class :" + ((ParameterizedType)type).getActualTypeArguments().length);  
    26.     }  
    27.       
    28. }  




    ===================== output =======================

    getClass()==class com.example.test.subParam


    type = com.example.test.param<com.example.test.myclass, com.example.test.myinvoke>


    trueType1 = class com.example.test.myclass


    trueType2 = class com.example.test.myinvoke


    B is A's super class :0



    总结

    ((ParameterizedType)type).getActualTypeArguments()

    是用来的到类的模板参数的类型的? 入T1, T2  etc...


    返回表示此类型实际类型参数的 Type 对象的数组。

  • 相关阅读:
    写了个限制文本框输入最大长度的jquery插件
    A2D JS框架
    在.Net中执行js
    C# Socket的粘包处理
    分布式EventBus的Socket实现
    读写分离子系统
    缓存子系统如何设计(Cachable tag, Memcache/redis support, xml config support, LRU/LFU/本地缓存命中率)
    pip install在Windows下报错解决
    Centos 6.9安装配置MongoDB
    Centos6.9安装Node.js+npm爬坑
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5920934.html
Copyright © 2011-2022 走看看