调用静态泛型方法 [英] Calling static generic methods
本文介绍了调用静态泛型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了一个涉及静态泛型方法的奇怪情况。这是代码:
class Foo< E>
{
public static< E> FOO< E - 代替; createFoo()
{
// ...
}
}
class Bar< E>
{
private Foo< E>会员;
public Bar()
{
member = Foo.createFoo();
$ / code $ / pre
我怎么没有指定任何类型表达式中的参数 Foo.createFoo()
?这是某种类型推断吗?如果我想明确它,我该如何指定类型参数?
解决方案是的,这是基于该分配的目标,根据 JLS部分15.12.2.8 。明确地说,你可以这样调用:
Foo。< String> createFoo();
I have come across a curious situation involving static generic methods. This is the code:
class Foo<E>
{
public static <E> Foo<E> createFoo()
{
// ...
}
}
class Bar<E>
{
private Foo<E> member;
public Bar()
{
member = Foo.createFoo();
}
}
How come I don't have to specify any type arguments in the expression Foo.createFoo()
? Is this some kind of type inference? If I want to be explicit about it, how can I specify the type argument?
解决方案 Yes, this is type inference based on the target of the assignment, as per JLS section 15.12.2.8. To be explicit, you'd call something like:
Foo.<String>createFoo();
这篇关于调用静态泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文