简体   繁体   English

Java:无法实现有界的泛型接口

[英]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 类型参数Comparable不在类型变量T#2的范围内,其中T#1,T#2是类型变量:T#1扩展了在类ArrayPriorityQueue中声明的对象T#2扩展了在PriorityQueueInterface接口中声明的Comparable

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> {...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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