Java父类取得子类传递的泛型参数Class类型

import java.lang.reflect.ParameterizedType;  
import java.lang.reflect.Type;  

import org.junit.Test;  

abstract class BaseDao<T> {  
    public void getType() {  
        //Class clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];  
        Type t = getClass().getGenericSuperclass();    
        ParameterizedType p = (ParameterizedType) t ;    
        Class c = (Class) p.getActualTypeArguments()[0];  
        System.out.println(c.getName());    //java.lang.String  
    }  
}  

public class SubDao extends BaseDao<String> {  
    @Test  
    public void test1() {  
        getType();  
    }  
}  

你可能感兴趣的:(J2EE,J2SE,Mybatis)