Explanation of generic > in collection.sort/ comparable code?

前端 未结 2 718
抹茶落季
抹茶落季 2020-11-28 23:52

I use comparable interface all the time to provided natural ordering for my class through collection.sort.

Basically if I have a person class, I will get it to imple

2条回答
  •  隐瞒了意图╮
    2020-11-29 00:34

    You always use extends with generics wildcards, even if the type parameter implements an interface.

    If you look at a class that implements Comparable, you'll see that it actually (should) implement Comparable, where T is the class itself.

    It makes sense if you think about the type paramter passed to the Comparable interface and how it's used in the compareTo() method.

    As PM 77-1 has eloquently pointed out, the super keyword allows for either the class, T, or one of its parents to implement Comparable.

提交回复
热议问题