Public Class MyStack
Private element() As Integer " create a dynamic array
Private pointer As Integer
Public Sub New(ByVal size As Integer)
ReDim element(size - 1)
pointer = 0
End Sub
Public Sub Push(ByVal item As Integer)
If pointer > UBound(element) Then
Throw New Exception("Stack is full.")
End If
element(pointer) = item
pointer += 1
End Sub
Public Function Pop() As Integer
pointer -= 1
If pointer < 0 Then
Throw New Exception("Stack is empty.")
End If
Return element(pointer)
End Function
End Class
Public Class MyStack
Private element() As Object " 후기 바인딩을 위해 Object 형식을 사용
Private pointer As Integer
Public Sub New(ByVal size As Integer)
ReDim element(size - 1)
pointer = 0
End Sub
Public Sub Push(ByVal item As Object)
If pointer > UBound(element) Then
Throw New Exception("Stack is full.")
End If
element(pointer) = item
pointer += 1
End Sub
Public Function Pop() As Object
pointer -= 1
If pointer < 0 Then
Throw New Exception("Stack is empty.")
End If
Return element(pointer)
End Function
End Class
Dim stackC As New MyStack(2)
stackC.Push(5)
stackC.Push("A")
Dim val1, val2 As Integer
val1 = stackC.pop "runtime error here
Public Class MyStack(Of itemType)
Private element() As itemType " 동적 배열 생성
Private pointer As Integer
Public Sub New(ByVal size As Integer)
ReDim element(size - 1)
pointer = 0
End Sub
Public Sub Push(ByVal item As itemType)
If pointer > UBound(element) Then
Throw New Exception("Stack is full.")
End If
element(pointer) = item
pointer += 1
End Sub
Public Function Pop() As itemType
pointer -= 1
If pointer < 0 Then
Throw New Exception("Stack is empty.")
End If
Return element(pointer)
End Function
End Class
Dim stackA As New MyStack(Of Integer)(2)
" the itemType will now be replaced by the Integer
stackA.Push(5)
stackA.Push(6)
stackA.Push("Some string...") " 컴파일 에러(compile-time error)
MsgBox(stackA.pop)
MsgBox(stackA.pop)
MsgBox(stackA.pop)
Dim stackB As New MyStack(Of String)(3)
stackB.Push("Generics")
stackB.Push("supports ")
stackB.Push("VB.NET ")
MsgBox(stackB.pop)
MsgBox(stackB.pop)
MsgBox(stackB.pop)
Public Class MyStack(Of itemType As Employee)
Public Class MyStack(Of itemType As {Employee, IComparable})
Public Class Employee
Implements IComparable
Public employeeID As String
Public name As String
Public Function CompareTo(ByVal obj As Object) As Integer _
Implements System.IComparable.CompareTo
" add code here
End Function
End Class
Public Class Manager
Inherits Employee
...
End Class
Public Class Programmer
Inherits Employee
...
End Class
Dim stackD As New MyStack(Of Programmer)(3)
Dim stackE As New MyStack(Of Employee)(3)
Dim stackF As New MyStack(Of Manager)(3)
이전 글 : 네트워크 분석을 위한 명령줄 도구
다음 글 : XP SP2 살펴보기
최신 콘텐츠