目录结构
User
package com.xiao.entity;
public class User {
}
父类
package com.xiao;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class T1 <T>{
public T1() {
Class<? extends T1> aClass = getClass();
System.out.println("getClass():"+aClass);
Type genericSuperclass = aClass.getGenericSuperclass();
System.out.println("genericSuperclass: "+ genericSuperclass);
ParameterizedType genericSuperclass1 = (ParameterizedType) genericSuperclass;
System.out.println("参数化 genericSuperclass1: "+genericSuperclass1);
Type actualTypeArgument = genericSuperclass1.getActualTypeArguments()[0];
System.out.println("actualTypeArgument: "+ actualTypeArgument);
String typeName = actualTypeArgument.getTypeName();
System.out.println("typeName: "+typeName);
String s = actualTypeArgument.toString();
System.out.println("s:" + s);
Class<? extends Type> aClass1 = actualTypeArgument.getClass();
System.out.println("aClass1: " + aClass1);
Class<T> actualTypeArgument1 = (Class<T>) actualTypeArgument;
System.out.println("actualTypeArgument1: "+ actualTypeArgument1);
Class<? extends Class> actualTypeArgument1Class = actualTypeArgument1.getClass();
System.out.println("actualTypeArgument1Class: " + actualTypeArgument1Class);
String simpleName = actualTypeArgument1.getSimpleName();
System.out.println("simpleName: "+simpleName);
}
}
子类
package com.xiao;
import com.xiao.entity.User;
public class T2<T> extends T1<User> {
public static void main(String[] args) {
T2 t = new T2();
}
}
祝你好运!
希望能帮你理解些!注释的内容可以不用看,本人随便写的个人感受。 下面部分的内容不用看了!!!! 下面部分的内容不用看了!!!!
有些说法可能不太合理,希望多多参考,我也是今天开始了解这个java反射机制。之前,老师也有将,没怎么用?所以没怎么了解,等真正需要的时候才肯去学,肯了解。多看看有些的开源项目,拿来学学?多上网搜搜,我这个也是参考别人的代码,让后试着去理解,并标记,我的方法可能有误,当然,目前本人根据自己还是这个行业的门外汉。哎!
|