Java 参数多态性

Java 参数多态性,java,generics,parametric-polymorphism,Java,Generics,Parametric Polymorphism,除了为分配实施行李和清单外,下一步是创建订购版本。要求使用正确的类型参数和约束指定参数化接口OrderedCollection。我的问题是如何实现它 存在一个接口集合,该接口定义为 public interface Collection<E> extends Iterable<E>{ public void add(E e); public void remove(E e); public boolean contains(Object e); publi

除了为分配实施行李和清单外,下一步是创建订购版本。要求使用正确的类型参数和约束指定参数化接口OrderedCollection。我的问题是如何实现它

存在一个接口
集合
,该接口定义为

public interface Collection<E> extends Iterable<E>{
  public void add(E e);
  public void remove(E e);
  public boolean contains(Object e);
  public void clear();
  public int size();
  public boolean isEmpty();
  public Object[] toArray();
}
因为它扩展了
集合
中已经定义的方法,并且唯一需要的新功能是
compareTo()
方法

但是,当我试图通过声明实现
OrderedList

public class OrderedList<E> extends UnorderedList<E> implements OrderedCollection<E>
公共类OrderedList扩展UnorderedList实现OrderedCollection 我得到一个错误的陈述

Bound mismatch: The type E is not a valid substitute for the bounded parameter <E
extends Comparable<E>> of the type OrderedCollection<E>
绑定不匹配:类型E不是OrderedCollection类型的绑定参数的有效替代品
根据我对错误消息的理解,我需要指定一个参数类型,它是接口声明中给出的参数类型的有效替代品。然而,我已经试过了

OrderedCollection<E extends Comparable<E>>
OrderedCollection
由于实现了declarer,但是我得到一个警告,extends上存在语法错误


如何满足此处的要求?

在您对
OrderedList
类的声明中,
OrderedList
的泛型类型需要与OrderedCollection期望的限制相匹配

public class OrderedList<E extends Comparable<E>> 
             extends UnorderedList<E> 
             implements OrderedCollection<E>
公共类OrderedList
扩展无序列表
实现OrderedCollection

我建议尝试使用公共类OrderedList扩展UnorderedList实现OrderedCollection。-注意:你确定公共int比(E);在你的收藏里?该方法应该做什么?需求在哪里?什么是问题?字符串“OrderedCollection”中没有语法错误。@itun,问题在于完整的OrderedList类声明中,E被声明为绑定错误,因为它不能替代接口的E扩展可比参数。约翰·哈格尔的回答帮了大忙,消除了错误。
OrderedCollection<E extends Comparable<E>>
public class OrderedList<E extends Comparable<E>> 
             extends UnorderedList<E> 
             implements OrderedCollection<E>