各位大神们,求救啊 ,如题,照着网上的方法写的出错了。
首先我声明了一个接口public interface BaseDal<T>,然后写了一个实现类public class EntityDal<T> implements BaseDal<T>。
在其中的一个方法,我需要用到T的class对象,于是我写了以下代码:
Type genType= this.getClass().getGenericInterfaces()[0];
ParameterizedType realType =(ParameterizedType)genType;
Type allTypes = realType.getActualTypeArguments();
Class<T> clazz = (Class<T>)allTypes[0];
在执行的时候,执行到最后一句就会报一个java.lang.ClassCastException: org.apache.harmony.luni.lang.reflect.ImplForVariable的错误,然后我打印一下相关信息,打印genType得到的BaseDal<T>,打印allTypes[0]输出的是T,打印allTypes[0].getClass输出的是org.apache.harmony.luni.lang.reflect.ImplForVariable,请问为什么allTypes[0]得到的类型不是Class类型的子类啊 ,问题出现在哪?应该怎么改啊 ?谢谢!
首先我声明了一个接口public interface BaseDal<T>,然后写了一个实现类public class EntityDal<T> implements BaseDal<T>。
在其中的一个方法,我需要用到T的class对象,于是我写了以下代码:
Type genType= this.getClass().getGenericInterfaces()[0];
ParameterizedType realType =(ParameterizedType)genType;
Type allTypes = realType.getActualTypeArguments();
Class<T> clazz = (Class<T>)allTypes[0];
在执行的时候,执行到最后一句就会报一个java.lang.ClassCastException: org.apache.harmony.luni.lang.reflect.ImplForVariable的错误,然后我打印一下相关信息,打印genType得到的BaseDal<T>,打印allTypes[0]输出的是T,打印allTypes[0].getClass输出的是org.apache.harmony.luni.lang.reflect.ImplForVariable,请问为什么allTypes[0]得到的类型不是Class类型的子类啊 ,问题出现在哪?应该怎么改啊 ?谢谢!









