如何从泛型类调用静态方法? [英] How to call static method from a generic class?
本文介绍了如何从泛型类调用静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class TestClass {
public静态< E>识别TestClass< E - 代替; create(){
返回新的TestClass< E>();
}
}
当我使用 TestClass.create()
,它可以被编译。但是当我使用 TestClass< String> .create()
时,它无法编译,如何指定泛型?
解决方案
假设您在案例类型推断失败时显式指定类型,可以使用 TestClass 。< String> create()
(注意类型在。
之后,与之前相比)。
I have a class containing a static create method.
public class TestClass {
public static <E> TestClass<E> create() {
return new TestClass<E>();
}
}
when I use TestClass.create()
, it can be compiled. But when I use TestClass<String>.create()
, it failed to compile, how to specify the generic?
解决方案
Assuming you are asking about specifying the type explicitly in case type inference fails, you can use TestClass.<String>create()
(notice how the type is after the .
as opposed to before).
这篇关于如何从泛型类调用静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文