简体   繁体   English

这是什么意思 <E, P extends Comparable<? super P> &gt;?

[英]What does this mean <E, P extends Comparable<? super P>>?

Anyways, I have been given a class and a PriorityQueueInterface to implement a Linked Node PriorityQueue. 无论如何,我已经获得了一个类和一个PriorityQueueInterface来实现链接节点PriorityQueue。 However, I'm having trouble grasping the class type. 但是,我在掌握类类型时遇到了麻烦。 here is the Entry class 这是入门班

I just don't know how to start the assignment without knowing what this means. 我只是不知道这意味着什么,不知道如何开始作业。

public class Entry<E, P extends Comparable<? super P>>
         implements Comparable<Entry<E, P>>
{
    private E theItem; 
    private P thePriority; 

    public Entry(E item, P priority)
    {
        theItem = item; 
        thePriority = priority;
    } 

    public E getItem()
    {
        return theItem;
    } 

    public P getPriority()
    {
        return thePriority;
    } 

    public int compareTo(Entry<E, P> other)
    {
        return thePriority.compareTo(other.thePriority);
    }

    public String toString()
    {
        return "item/priority <" + theItem + ", " + thePriority + ">";
    }
} 

Here is the Interface 这是界面

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

/** Adds a new entry to this priority queue. 
 * @param newEntry An object to be added */ 
 public void add(T newEntry); 

 /** Removes and returns the entry having the highest priority. 
  * @return Either the object having the highest priority or 
  *         if, the priority queue is empty before the operation, null. */ 
  public T remove(); 

  /** Retrieves the entry having the highest priority.
  @return  Either the object having the highest priority or,
           if the priority queue is empty, null. */
  public T peek(); 

  /** Detects whether this priority queue is empty.
  @return  True if the priority queue is empty, or false otherwise. */
  public boolean isEmpty(); 

   /** Gets the size of this priority queue.
  @return  The number of entries currently in the priority queue. */
  public int getSize(); 

  /** Removes all entries from this priority queue. */
   public void clear();

}// End of PriorityQueueInterface

Let's break this down: Entry<E, P extends Comparable<? super P>> implements Comparable<Entry<E, P>> 让我们分解一下: Entry<E, P extends Comparable<? super P>> implements Comparable<Entry<E, P>> Entry<E, P extends Comparable<? super P>> implements Comparable<Entry<E, P>> . Entry<E, P extends Comparable<? super P>> implements Comparable<Entry<E, P>> Think of Entry is a type (say A ). 认为Entry是一种类型(例如A )。 So the statement translates to A implements Comparable<A> . 因此,该语句转换为A implements Comparable<A> This means, this type can compare itself to other object of same type. 这意味着该类型可以将自己与其他相同类型的对象进行比较。

Now let's go deeper. 现在让我们更深入。 Entry has two parameters. 条目具有两个参数。 E and P . EP Easy. 简单。

Going further, P extends Comparable meaning P can compare itself to something. 更进一步, P extends Comparable意味着P可以将自己与某些事物进行比较。 The type P can compare itself to is given by the innermost <> which is ? super P 类型P可以比较自己的类型是由最里面的<>给出的? super P ? super P . ? super P This means P can compare itself to objects of type P or it's super class. 这意味着P可以将自身与P类型的对象或它的超类进行比较。

Putting everything together, you have an Entry of two parameters which should be able to compare itself to other entries of same parameters. 将所有内容放在一起,您将获得两个参数的Entry,它们应该能够与其他相同参数的条目进行比较。 One of those parameters is E . 这些参数之一是E The other one is P and P should be able to compare itself to any of it's super class objects. 另一个是PP应该能够将自己与其任何超类对象进行比较。

If you want to learn about when to write super and when to write extends , there are plenty of questions explaining that. 如果您想了解何时编写super以及何时编写extends ,那么有很多问题可以解释这一点。

The concrete class for P in Entry must implement Comparable . P in Entry的具体类必须实现Comparable Since Comparable is a generic as well, the declaration forces that P must implement Comparable over P with the <? super P> 由于Comparable也是通用的,因此声明强制P必须使用<? super P>在P上实现Comparable <? super P> <? super P> part. <? super P>部分。

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

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