Home  >  Q&A  >  body text

java - T extends Comparable<? super T> 中问号? 是固定的吗?

迷茫迷茫2766 days ago814

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 15:46:48

    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 <? extends T> and <? super T>.
    <? super T> 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.
    <? extends T> is called upward shaping. Taking ArrayList<? extends T> 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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:46:48

    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.

    reply
    0
  • Cancelreply