【问题标题】:Bound mismatch: The type MyClass1 is not a valid substitute for the bounded parameter <T extends Comparator<T>> of the type Person<T>绑定不匹配:类型 MyClass1 不是 Person<T> 类型的有界参数 <T extends Comparator<T>> 的有效替代品
【发布时间】:2018-10-01 04:05:11
【问题描述】:

我得到一个错误:::Bound mismatch: MyClass1 类型不是有界参数&lt;T extends Comparator&lt;T&gt;&gt; of the type Person&lt;T&gt; 的有效替代品

import java.util.Comparator;


public class Test
{

    public static void main(String[] args)
    {
        Person<MyClass1> person2 = new Person<>(); // Bound mismatch: The type MyClass1 is not a valid substitute for the bounded parameter <T extends Comparator<T>> of the type Person<T>
    }
}



class MyClass1<T extends Comparator<T>> implements Comparator<T>
{

    @Override
    public int compare(T o1, T o2)
    {
        return 0;
    }
}



class Person<T extends Comparator<T>>
{

}

【问题讨论】:

    标签: java generics


    【解决方案1】:

    要求是MyClass1需要匹配T extends Comparator&lt;T&gt;

    为此,它的声明应该是class MyClass1 extends Comparator&lt;MyClass1&gt;

    【讨论】:

    • class MyClass1 扩展 Comparator 。我想在 Comparator 接口中使用类型参数没有类型参数传递
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多