java 泛型与反射(5)

来源:互联网 发布:安装软件没反应 编辑:程序博客网 时间:2024/05/04 09:43

这里以一个dao类作为测试展示泛型与反射作用

public Dao<T>{     public Dao(){System.out.println("Dao's constructor ...");System.out.println(this);System.out.println(this.getClass());//获取Dao子类的父类Class cls2 = this.getClass().getSuperclass();System.out.println(cls2);//获取泛型父类//Type 是个空接口Type type = this.getClass().getGenericSuperclass(); //获取具体的泛型参数 //ParameterizedType实现了Type接口,该类中有getActualTypeArguments()方法可获取参数类型 if(type instanceof ParameterizedType){ ParameterizedType parameterizedType = (ParameterizedType)type; Type[] args = parameterizedType.getActualTypeArguments(); if(args != null && args.length > 0){Type arg = args[0];if(arg instanceof Class){ cls = (Class)arg;} }}}}


0 0