java generic: declaration of recursive types

后端 未结 2 721
攒了一身酷
攒了一身酷 2020-12-21 12:17

In Effevtive Java I see a declaration: public static > T max(List list), type variable T is a

相关标签:
2条回答
  • 2020-12-21 12:56

    That means that you will then expect a type that is comparable to Object, since java infers generic types to be Object when not provided.

    <T extends Comparable> // The generic type for Comparable is inferred as Object as it was not provided.
    

    This means that the type provided to the list would need to implement compareTo(Object o) and implement Comparable (notice the missing generic type to Comparable), which I assume is not intended.

    0 讨论(0)
  • 2020-12-21 13:02

    You would have your parametrized type declared to implement the Comparable interface in a "raw" way.

    For instance, a class Foo implements Comparable (raw type) would fit your parametrized type.

    In turn, that class could override compareTo(Object o) instead of compareTo(Foo f).

    0 讨论(0)
提交回复
热议问题