Why does this Java method appear to have two retur

2019-01-01 01:48发布

问题:

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

where Foo is my own class.

What is the return type of this method? Why does it seem to have two return types?

回答1:

No, you don\'t have two return types.It\'s a generic method you are seeing.

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

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

Your method can have a generic type E which is a sub-class of Foo. your return type is a List<Foo or any SubType Of FOO>



回答2:

The return type is List<E>. The clause <E extends Foo> is not a return type; it is a generic type declaration, specifying that the particular type E must be a Foo (or a subclass of Foo). This is standard syntax for declaring a generic method.



回答3:

Take a look at the Java documentation pertaining to generics.

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

The return type of the method is List<E>.



标签: java methods