java 什么是静态<T> List<T> methodName (List<? super T> input)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11303694/
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
What is static <T> List<T> methodName (List<? super T> input)
提问by user1459497
I have the following code but i am confused with all the generics.
我有以下代码,但我对所有泛型感到困惑。
public static <T> List<T> backwards (List<? super T> input) {
List<T> output = new ArrayList<T>();
return output;
}
My understanding is that I have a public method named backwards
which creates an arraylist implementing the List interface and returning the arraylist. My question is what actually I am saying to the compiler with the following part......
我的理解是,我有一个名为的公共方法backwards
,它创建一个实现 List 接口的数组列表并返回数组列表。我的问题是我实际上是在用以下部分对编译器说些什么......
static <T> List<T> backwards (List<? super T> input)
回答by Paul Bellora
You are saying to the compiler:
你对编译器说:
<T>
"I'm declaring an arbitrary type T
for this method, which can be anything (non-primitive) for each call of the method."
“我T
为这个方法声明了一个任意类型,对于方法的每次调用,它可以是任何(非原始)类型。”
List<T>
"This method will return a List
containing elements of that type T
."
“此方法将返回List
包含该类型的元素T
。”
List<? super T> input
"This method will take a parameter called input
, which is a List
containing elements of type T
, or any super-type of T
. For example, if T
is Integer
, input
could be a List<Integer>
, List<Number>
, or List<Object>
."
“这种方法将需要一个名为参数input
,这是一种List
类型的包含元素T
,或任何超类型的T
。例如,如果T
是Integer
,input
可以是一个List<Integer>
,List<Number>
或List<Object>
”。
回答by jax
This is how you declare Generic Methods. Please read the following part and it will give you everything you need.
这就是您声明泛型方法的方式。请阅读以下部分,它将为您提供所需的一切。
http://docs.oracle.com/javase/tutorial/extra/generics/methods.html
http://docs.oracle.com/javase/tutorial/extra/generics/methods.html