输出结果如下: class myGenetic.Person myGenetic.Person@10d448
function in Person.class...
package com.sourcefour.test.dao;
但是一般这样写就悲剧了... import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; class ACE extends TestJave<String>{
} public class TestJave<T> { private Class<T> entityClass; /** * 通过反射获取子类确定的泛型类 */ public TestJave() { Type[] params =((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments(); }
public static void main(String[] args) { // TestJave<?> te = new ACE(); TestJave<String> te = new TestJave<String>(); //运行错误!! } }
Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType at com.sourcefour.test.dao.TestJave.<init>(TestJave.java:16) at com.sourcefour.test.dao.TestJave.main(TestJave.java:21)