Inferred type is not a valid substitute for a Comparable generic type

前端 未结 4 1966
感动是毒
感动是毒 2021-01-11 11:11

Consider the code:

public abstract class Item implements Comparable
{
    protected T item;

    public int compareTo(T o)
    {
        re         


        
4条回答
  •  情话喂你
    2021-01-11 11:49

    For objects of type X to be comparable with each other, class X has to implement exactly Comparable.

    This is not what your code is doing, you've got a class Item and you are implementing Comparable instead of Comparable>. This means that Item can be compared with T, but not with Item, which is required.

    Change your Item class to:

    public abstract class Item implements Comparable>
    {
        protected T item;
    
        @Override
        public int compareTo(Item o)
        {
            return 0; // this doesn't matter for the time being
        }
    }
    

提交回复
热议问题