java – 泛型和比较器

我正在研究泛型,发现以下代码在比较方法时给出了编译时错误.

Multiple markers at this line
– Cannot infer type argument(s) for comparing(Function)
– The type A does not define m1(Object) that is applicable here

class A<T> {
    String m1() {
        return null;
    }
}

class B {
    void test() {
        Comparator<A<String>> target = Comparator.comparing(A::m1).thenComparing(A::m1);
    }
}

有人可以帮助我理解这种行为;我该如何解决这个问题呢?

解决方法

如果在比较方法中指定了确切的泛型类型,则代码将进行编译.

Comparator<A<String>> target =
    Comparator.<A<String>,String>comparing(A::m1).thenComparing(A::m1);

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...