简体   繁体   English

重写了“ Java有效”示例中通用方法的绑定不匹配

[英]Bound mismatch for generic method from “Java Effective” example rewritten

class Main {
public static void main(String[] args) {
    List<Sub> list = new ArrayList<Sub>();

    Sub r = max(list);

    System.out.println(r);
}

static <T extends Comparable<? super T>> T max(List<? extends T> list) {
    return null;
}

private static class Super {
}

private static class Sub implements Comparable<Super> {
    public int compareTo(Super o) {
        return 0;
    }
}
}

Can anyone tell me why I'm getting compiler error at line "Sub r = max(list)"? 谁能告诉我为什么我在“ Sub r = max(list)”行出现编译器错误?

I'm reading Java Effective book and I got at, as they said, the most complex method declaration in that book. 我正在阅读Java Effective书,正如他们所说,我得到了书中最复杂的方法声明。 It's actually that max method. 实际上就是max方法。

Error is: 错误是:

Bound mismatch: "The generic method max(List< ? extends T>) of type Main is not applicable for the arguments (List< Main.Sub>). The inferred type Main.Sub is not a valid substitute for the bounded parameter < T extends Comparable< ? super T>>"

您的Sub类必须扩展Super

private static class Sub extends Super implements Comparable<Super> {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM