开发者

What does the ? mean in "format(List<? extends NameValuePair>"

I'm following the indications on this post to create a a parametrized URI for an Http GetMethod (http://some.domain/path?param1=value1&param2=value2) and I ran into a new issue.

I have this code:

List<NameValuePair> params = new ArrayList<NameValuePair>();
...
some code which does: params.add(new BasicNameValuePair(paramName, paramValue));
...
String paramString = URLEncodedUtils.format(params,开发者_如何学Python "utf-8");

However, my Eclipse says that URLEncodeUtils.format does not accept type List<NameValuePair> but only List<? extends NameValuePair>.

I thought this may mean that only a subclass would be accepted (I see no sense in it, though, since NameValuePair is not abstract) but I also tried doing this with no luck:

class NameValuePairExtension extends NameValuePair{}
List<NameValuePairExtension> params = new ArrayList<NameValuePairExtension>();

What does this exactly mean?

EDIT:

Thanks for your quick replies.

A part from the theorical repsonses, I just found the "practical solution": NameValuePair is an Interface, so it format connot work with it, and requires an implementation.

Therefore, the solution is to replace NameValuePair by a BasicNameValuePair, which implements the first one.

However, I still find this "extends" a bit confusing.


Xxx<? extends Yyy> means that Xxx has a generic type parameter that can be whatever you want while it implements interface Yyy (if Yyy is an interface) or extends class Yyy (if Yyy is a class).


It means it's a list of some type which extends NameValuePair (or is NameValuePair) but we don't know what type, and we don't care.

This is a wildcard in Java generics. See the Java Generics FAQ for more details.

EDIT: My guess is that you're using the wrong NameValuePair type. Please post the exact compilation error and it may become clearer...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜