[英]Generic type declaration caused an error
To be brief, I have: 简而言之,我有:
public interface Dictionary<E extends Comparable<E>> extends Iterable<E> {
And 和
public class TreeDictionary implements Dictionary<TreeDictionary> {
When compiled, caused an error 编译时,导致错误
TreeDictionary.java:4: type parameter TreeDictionary is not within its bound public class TreeDictionary implements Dictionary {
TreeDictionary.java:4:类型参数TreeDictionary不在其绑定的公共类之内TreeDictionary实现Dictionary {
I guess the reason is probably because I'm not fully clear with the declaration of the Dictionary interface. 我想原因可能是因为我对Dictionary接口的声明不完全清楚。
Would really appreciate if somebody could explain me this :) 如果有人可以向我解释一下,我将不胜感激:)
public interface Dictionary<E extends Comparable<E>>
That says that whichever type you want to use for E
, it must implement the Comparable
interface. 也就是说,无论您要用于
E
哪种类型,都必须实现Comparable
接口。
Given your TreeDictionary
definition: 给定您的
TreeDictionary
定义:
public class TreeDictionary implements Dictionary<TreeDictionary>
TreeDictionary
does not implement Comparable
, and so cannot be substituted for E
in the generic type of Dictionary
. TreeDictionary
没有实现Comparable
,因此不能用Dictionary
的通用类型替换E
Try this instead: 尝试以下方法:
public class TreeDictionary implements Dictionary<TreeDictionary>, Comparable<TreeDictionary>
TreeDictionary
should now conform to the constraints of E
. TreeDictionary
现在应该符合E
的约束。
您的TreeDictionary类还应该实现Comaparable接口。
You need implement Comparable interface for it to work 您需要实现Comparable接口才能使其正常工作
public class TreeDictionary implements Dictionary, Comparable 公共类TreeDictionary实现Dictionary,具有可比性
hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.