简体   繁体   中英

Java: Having trouble implementing bounded generics interface

I searched through a lot of questions and other internet articles, but I can't seem to find the one that caters to my specific case, and none of the other ones solutions worked for me.

I have this interface here:

public interface PriorityQueueInterface<T extends Comparable<? super T>>

I need to make a priority queue class to implement this interface, so I typed it out like this:

public class ArrayPriorityQueue<T> implements PriorityQueueInterface<Comparable<? super T>>

However, it is not compiling as I get this error:

type argument Comparable is not within bounds of type-variable T#2 where T#1,T#2 are type-variables: T#1 extends Object declared in class ArrayPriorityQueue T#2 extends Comparable declared in interface PriorityQueueInterface

I tried all types of combinations, but nothing seems to work. How do I write the class declaration so that it compiles?

Seems like what you want is to declare the type variable with the same bound, then pass that on as an argument to the interface:

public class ArrayPriorityQueue<T extends Comparable<? super T>>
    implements PriorityQueueInterface<T> {...}

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