java - T extends Comparable 中问号? 是固定的吗?
迷茫
迷茫 2017-04-17 15:44:48
0
2
778
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (2)
黄舟

is fixed and represents T or T’s parent class.

The original intention of Java's definition of generics is to ensure that errors that occur during runtime can be checked early at compile time. With this understanding, let's look at and .
is called downward modeling, which means that the generic elements in the Comparable interface are all T or the parent class of T. When passing in parameters, you can pass in T or the subclass of T.
is called upward shaping. Taking ArrayList as an example, it means that all elements in this list collection are subclasses of T and can be automatically converted to T type when taken out.

It’s a bit hard to explain, but it’s actually not that complicated. You’ll understand it after a few knocks.

    Peter_Zhu

    If you want to get data from a data type, use the ? extends wildcard;
    If you want to write the object into a data structure, use the ? super wildcard;
    If you want to both save and retrieve, then don’t use it Wildcard.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!