我想實現優先級隊列類。當一個項目以更高的優先級被添加時,它被推到隊列的前面,而不是添加到隊列的末尾。代碼從列表(T)繼承的問題類
Public Class PriorityQueue(Of T)
Inherits List(Of T)
Private _list As New List(Of T)
Public Sub Enque(ByVal item As T, Optional ByVal pushToFront As Boolean = False)
If pushToFront = True Then
_list.Insert(0, item)
Else
_list.Add(item)
End If
End Sub
Public Function Deque() As T
If _list.Count <> 0 Then
Dim item As T = _list(0)
_list.RemoveAt(0)
Return item
Else
Throw New InvalidOperationException
End If
End Function
End Class
簡單的幾行字現在調用函數試圖找到在隊列中的元素從而 ....
dim _q as new PriorityQueue(Of integer)
_q.Enque(1)
_q.Enque(2)
msgbox(_q.Count())
.....
該程序打印出0!如果添加一個Count()屬性,那麼一切都很好。 我原以爲繼承的類應該調用基類的Count函數。 請注意,即使我在派生類中沒有實現,Count也會顯示在intellisense中。
請標記爲代碼:「dim _q as new PriorityQueue(Of integer)_q.Enque(1)_q.Enque(2)msgbox(_q.Count())」 – 2009-04-27 18:29:38