標籤:color super base nis system ase 參數化 type param
If the superclass is a parameterized type, the {@code Type} * object returned must accurately reflect the actual type * parameters used in the source code. T
上文檔DOC,如果父類是一個參數化型別,那麼Type返回的是參數類型的真實類型
package entity;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;/** * Created by Administrator on 2016/11/27 0027. */public class BaseDaoImpl<T> implements BaseDao<T> { public BaseDaoImpl(){ Type type = getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType)type).getActualTypeArguments(); System.out.println((Class<T>)params[0]); } public T findByid(int id) { return null; }}
package entity;/** * Created by Administrator on 2016/11/27 0027. */public class UserDaoImpl extends BaseDaoImpl<User> {}
package entity;/** * Created by Administrator on 2016/11/27 0027. */public interface BaseDao<T> { public T findByid(int id);}
package entity;import org.junit.Test;/** * Created by Administrator on 2016/11/27 0027. */public class GenericTest { @Test public void testGeneric(){ UserDaoImpl baseDao = new UserDaoImpl(); //BaseDaoImpl<User> baseDao1 = new BaseDaoImpl<User>(); }}
記錄總結,只有繼承的父類為參數化型別,此時反射的
ParameterizedType 才能拿到參數類型
Java的泛型反射