简体   繁体   中英

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 {

I guess the reason is probably because I'm not fully clear with the declaration of the Dictionary interface.

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.

Given your TreeDictionary definition:

public class TreeDictionary implements Dictionary<TreeDictionary>

TreeDictionary does not implement Comparable , and so cannot be substituted for E in the generic type of Dictionary .

Try this instead:

public class TreeDictionary implements Dictionary<TreeDictionary>, Comparable<TreeDictionary>

TreeDictionary should now conform to the constraints of E .

您的TreeDictionary类还应该实现Comaparable接口。

You need implement Comparable interface for it to work

public class TreeDictionary implements Dictionary, Comparable

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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