Java -- 泛型父类中获取子类的泛型T

原文:https://blog.csdn.net/u014723529/article/details/70574026

/**
 * 获取实体类型名称
 * 子类可覆盖此方法,返回:泛型T的类名.class.getSimpleName()
 * @return
 */
protected String getModelName() {
    Type t = getClass().getGenericSuperclass();    
    ParameterizedType p = (ParameterizedType) t ;    
    Class<T> c = (Class<T>) p.getActualTypeArguments()[0]; 
    return c.getSimpleName();
}

猜你喜欢

转载自www.cnblogs.com/yarightok/p/11163145.html