I want to change the return type of this method to a class (ReturnContainer
) that can hold the tupelo
object in addition to another return value that I need to send back to the calling method.
我想将此方法的返回类型更改为一个类(ReturnContainer),它可以保存tupelo对象,并将我需要返回的另一个返回值返回给调用方法。
I've never worked with this <T>
concept in Java before so I don't how to reconfigure this method to work the way I need it to.
我以前从来没有在Java中使用过这个< t>概念,所以我不知道如何重新配置这个方法以达到我所需要的方式。
public static <T extends LocationCapable> List<T> test(Class<T> incomingClass)
{
List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
return tupelo;
}
When I try to change the code to the listing below, I get the error:
当我尝试将代码更改为下面的列表时,我得到了错误:
T cannot be resolved to a type
How can I have a return type of ReturnContainer
but still allow the incomingClass to be a dynamic type?
我如何有返回类型的ReturnContainer,但仍然允许incomingClass是动态类型?
public static ReturnContainer test(Class<T> incomingClass)
{
List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
return rc;
}
1 个解决方案
#1
6
You're missing the type parameter:
您缺少类型参数:
public static <T> ReturnContainer test(Class<T> incomingClass)
{
List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
return rc;
}
To my eyes, though, this looks weird. Shouldn't ReturnContainer
have a type parameter, too? In which case, you'd have
但在我看来,这看起来很奇怪。ReturnContainer不应该也有类型参数吗?在这种情况下,你会
public static <T> ReturnContainer<T> test(Class<T> incomingClass)
{
List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
ReturnContainer rc = new ReturnContainer<T>(tupelo, incomingClass);
return rc;
}
#1
6
You're missing the type parameter:
您缺少类型参数:
public static <T> ReturnContainer test(Class<T> incomingClass)
{
List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
return rc;
}
To my eyes, though, this looks weird. Shouldn't ReturnContainer
have a type parameter, too? In which case, you'd have
但在我看来,这看起来很奇怪。ReturnContainer不应该也有类型参数吗?在这种情况下,你会
public static <T> ReturnContainer<T> test(Class<T> incomingClass)
{
List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
ReturnContainer rc = new ReturnContainer<T>(tupelo, incomingClass);
return rc;
}