调用静态泛型方法

编程入门 行业动态 更新时间:2024-10-28 10:22:21
本文介绍了调用静态泛型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一个涉及静态泛型方法的奇怪情况。这是代码:

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();

更多推荐

调用静态泛型方法

本文发布于:2023-11-10 10:25:27,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!