简体   繁体   English

通用类型声明导致错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM