简体   繁体   English

从方法返回泛型

[英]Return generic from method

How I can return generic type from method? 如何从方法返回通用类型? I wrote like this, but it dont work: 我这样写,但是不起作用:

private static JsArray<T> convertListToJsArray(Collection<T> list)

I need to get Collection as parameter, and return parametrized JsArray. 我需要将Collection作为参数,并返回参数化的JsArray。

您必须在返回类型前面指定type参数:

private static <T> JsArray<T> convertListToJsArray(Collection<T> list) {}

You need to declare the T type. 您需要声明T类型。 This should work (notice the extra <T> before your return type): 这应该可以工作(请在返回类型之前注意多余的<T> ):

private static <T> JsArray<T> convertListToJsArray(Collection<T> list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM