static <T extends Number & Comparable<? super Number>>

依然范特西╮ 提交于 2019-12-05 08:25:12

Try <T extends Number & Comparable<T>>.

E.g. Integer implements Comparable<Integer>, which is not compatible with Comparable<? super Number> (Integer is not a superclass of Number). Comparable<? extends Number> would not work either because Java would then think the ? could be any subclass of Number, and passing a T to compareTo would then not compile because it expects a parameter of ?, not T.

Edit: as newacct said, <T extends Number & Comparable<? super T>> will work too (and be slightly more general) since then compareTo will then accept any ? of which T is a subclass, and as usual, an instance of a subclass can be given as a parameter where a superclass is expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!