java抽象类中反射机制的使用(求大神速来,现场结帖)

mmxiaowang 2019-06-09 08:15:40

public abstract class BaseDaoImpl<T> implements BaseDao<T> {
/** 当前操作的实体的类型信息 */
protected Class clazz;
/** 实体对应的表名 */
protected String tableName;

protected QueryRunner qr = new QueryRunner();
protected BeanHandler<T> beanHandler;
protected BeanListHandler<T> beanListHandler;
protected ScalarHandler<Long> scalarHandler;

public BaseDaoImpl() {
/**
* 反射的本质是得到class对象后,反向获取类对象的各种信息
*/
// 通过反射机制获取子类传递过来的实体类的类型信息
ParameterizedType type = (ParameterizedType) this.getClass()
.getGenericSuperclass();
System.out.println(type);
clazz = (Class) type.getActualTypeArguments()[0];

beanHandler = new BeanHandler<T>(clazz);
beanListHandler = new BeanListHandler<T>(clazz);
scalarHandler = new ScalarHandler<Long>();

this.tableName = clazz.getSimpleName().toLowerCase();
}


代码如上,其中下面这句中的getGenericSuperclass()是子类调用还是抽象类自身调用,我测试了发现返回值是
com.tomo.dao.impl.common.BaseDaoImpl<com.tomo.entity.Shop>
求大神解答。
ParameterizedType type = (ParameterizedType) this.getClass()
.getGenericSuperclass();

...全文
78 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧