public interface ParameterizedType extends Type
subParam.Java
- package com.example.test;
- public class subParam extends param<myclass, myinvoke> {
- public static void main(String[] args) throws Exception{
- subParam s = new subParam();
- }
- }
param.java
- package com.example.test;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- public class param<T1, T2> {
- class A {}
- class B extends A {}
- private Class<T1> entityClass;
- protected param (){
- Type type = getClass().getGenericSuperclass();
- System.out.println("getClass()==" + getClass());
- System.out.println("type = " + type);
- Type trueType = ((ParameterizedType)type).getActualTypeArguments()[0];
- System.out.println("trueType1 = " + trueType);
- trueType = ((ParameterizedType)type).getActualTypeArguments()[1];
- System.out.println("trueType2 = " + trueType);
- this.entityClass = (Class<T1>)trueType;
- B t = new B();
- type = t.getClass().getGenericSuperclass();
- System.out.println("B is A's super class :" + ((ParameterizedType)type).getActualTypeArguments().length);
- }
- }
===================== 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 对象的数组。