无法通过泛型访问类方法 [英] Cannot access class methods via generics
本文介绍了无法通过泛型访问类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将我的方法更改为通用方法。现在发生的是,我在反序列化 methodB
中的类,并访问它的方法,我不能再做了。
I changed my method to generic method. What is happening now is that I was deserializing the class inside the methodB
and accessing its methods which I can not do anymore.
<T> void methodB(Class<T> clazz) {
T var;
HashMap<String, T> hash = new HashMap<>();
}
void methodA () {
methodB(classA.class);
}
最初位于 methodB
没有泛型,
var = mapper.convertValue(iter.next(), ClassA.class);
var.blah() //works fine
使用泛型后,
var = mapper.convertValue(iter.next(), clazz);
var.blah() //cannot resolve the method.
如何访问 blah()
classA
?
推荐答案
感谢在Java中为类属参数传递类型参数为类型参数的类。使用 TypeToken 解决
这篇关于无法通过泛型访问类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文