为什么这个Java方法似乎有两种返回类型?

时间:2013-03-07 21:16:01

标签: java methods

public <E extends Foo> List<E> getResult(String s);

其中Foo是我自己的班级。

此方法的返回类型是什么?为什么它似乎有两种返回类型?

3 个答案:

答案 0 :(得分:20)

不,你没有两种返回类型。你看到的是generic method

 <E extends Foo> --> you are declaring a generic type for your method

 List<E> --> this is your return type

您的方法可以使用通用类型E,它是Foo的子类。您的退货类型为List<Foo or any SubType Of FOO>

答案 1 :(得分:6)

返回类型为List<E>。条款<E extends Foo>不是返回类型;它是泛型类型声明,指定特定类型E必须是Foo(或Foo的子类)。这是声明泛型方法的标准语法。

答案 2 :(得分:3)

查看与泛型有关的Java documentation

<E extends Foo> // declares the bounds for the generic type `E`
List<E> // declares the return value

方法的返回类型为List<E>