java <T> 无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3777315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<T> cannot be resolved to a type
提问by Dieter Gantz
I want to change the return type of this method to a class (ReturnContainer) that can hold the tupeloobject 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.
我以前从未<T>在 Java 中使用过这个概念,所以我不知道如何重新配置这个方法以按照我需要的方式工作。
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 ReturnContainerbut 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;
}
回答by gustafc
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 ReturnContainerhave 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;
}

