I don’t write Java, but all generics are interlinked. I happened to be looking at Taobao OPEN's SDK at this time. This is a real example that best explains generic wildcards.
I specially found a Java version of SDK, see: https://github.com/ggd543/tao...
All API requests are made through subclasses of execute 来操作的,而这个方法就采用的 ? extends T 通配符上界,来限制返回的对象必须是 TaobaoResponse.
To put it simply, it is a constraint.
From the perspective of Taobao SDK, the greatest value of the wildcard upper bound is that the results returned by all Taobao SDK requests will have a code、msg and other common parameters to indicate the request status of the API.
Okay, let’s go back and look at the specific implementation of execute:
Remember this sentence from the book Effective Java: Producer Extends, Consumer Super.
It’s explained in detail here: http://stackoverflow.com/ques...
Supplement
TreeMap
has a constructor:You can think about why you need to specify the type Comparator<? super K> instead of Comparator<K> or Comparator<? extends K>?
I don’t write Java, but all generics are interlinked. I happened to be looking at Taobao OPEN's SDK at this time. This is a real example that best explains generic wildcards.
I specially found a Java version of SDK, see: https://github.com/ggd543/tao...
All API requests are made through subclasses of
execute
来操作的,而这个方法就采用的? extends T
通配符上界,来限制返回的对象必须是TaobaoResponse
.To put it simply, it is a constraint.
From the perspective of Taobao SDK, the greatest value of the wildcard upper bound is that the results returned by all Taobao SDK requests will have a
code
、msg
and other common parameters to indicate the request status of the API.Okay, let’s go back and look at the specific implementation of
execute
:Notice the subclass of
catch
体中localResponse
,他的类型T
,但这个T并不是简单的一个Object对象,由于前面已经限定T
的类型必须是TaobaoResponse
here.That means, when the definition of
T
进行实例后,其类型至少是TaobaoResponse
的子类,而对于TaobaoResponse
is this:So you can directly see code like this:
Look, isn’t it interesting? All common parameters can be processed uniformly.
So from the perspective of Taobao SDK, this is the meaning of generic wildcards.