2017-07-20 42 views
0

無論如何,我已經給了一個類和一個PriorityQueueInterface來實現一個Linked Node PriorityQueue。但是,我無法掌握類的類型。 這裏是入門級這是什麼意思<E,P擴展可比較的<? super P>>?

我只是不知道如何開始任務而不知道這意味着什麼。

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 + ">"; 
    } 
} 

這裏是接口

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 
+0

你對泛型有一些想法嗎?佩奇? –

回答

1

讓我們來分析一下:Entry<E, P extends Comparable<? super P>> implements Comparable<Entry<E, P>>。認爲輸入是一種類型(比如A)。所以聲明轉換爲A implements Comparable<A>。這意味着,這種類型可以將自己與其他同類型的對象進行比較。

現在讓我們來深入。輸入有兩個參數。 EP。簡單。

更進一步,P extends Comparable含義P可以比較自己的東西。類型P可以比較自己是由最內層<>,這是? super P。這意味着P可以將其自身與P類型的對象進行比較,或者它是超類。

把所有東西放在一起,你有兩個參數的輸入,它應該能夠將自己與其他相同參數的輸入進行比較。其中一個參數是E。另一個是PP應該能夠將自身與它的任何超類對象進行比較。

如果您想了解何時寫super以及何時寫extends,有很多問題可以解釋。

+0

非常感謝你的解釋 – RussOT

0

對於P在Entry具體的類必須實現Comparable。由於Comparable也是通用的,因此聲明強制P必須實施Comparable而不是P與<? super P>部分。

相關問題