Наследование и переопределение, vb.net - PullRequest
0 голосов
/ 15 марта 2019

У меня есть класс, который наследует другой класс, в моем родительском классе есть переопределяемая функция, а в подклассе есть переопределяющая функция. Обе эти функции имеют одинаковое имя. Однако, когда я вызываю функцию в объекте подкласса, родительская функция и функция в подклассе запускаются после друг друга. Я только хочу, чтобы выполнялась переопределяющая функция (в подклассе), как мне это исправить?

Родительским классом является «очередь», а подклассом - «круговая очередь». Я хочу переопределить как функцию enqueue, так и функцию dequeue.

Модуль Модуль1

Sub Main()
    Dim sorq As String
    Dim schoice As String
    Dim qchoice As String
    Dim circhoice As String
    Dim prioritychoice As String
    Dim st As New stack
    Dim qu As New queue
    Dim cir As New movingQueue
    Dim pr As New priorityQueue
    Do
        menu1()
        sorq = UCase(Console.ReadLine())
        Select Case sorq
            Case "A"
                Do
                    stackmenu()
                    schoice = UCase(Console.ReadLine)
                    Select Case schoice
                        Case "A"
                            st.push()
                        Case "B"
                            st.pop()
                        Case "C"
                            st.peek()
                        Case "D"
                            st.output()
                        Case "E"
                            st.full()
                        Case "F"
                            st.empty()
                    End Select
                Loop Until schoice = "X"
            Case "B"
                Do
                    queuemenu()
                    qchoice = UCase(Console.ReadLine)
                    Select Case qchoice
                        Case "A"
                            qu.enqueue()
                        Case "B"
                            qu.dequeue()
                        Case "C"
                            qu.output()
                        Case "D"
                            qu.full()
                        Case "E"
                            qu.empty()
                    End Select
                Loop Until qchoice = "X"
            Case "C"
                Do
                    circularmenu()
                    circhoice = UCase(Console.ReadLine)
                    Select Case circhoice
                        Case "A"
                            cir.enqueue()
                        Case "B"
                            cir.dequeue()
                        Case "C"
                            cir.output()
                        Case "D"
                            cir.full()
                        Case "E"
                            cir.empty()
                    End Select
                Loop Until circhoice = "X"
            Case "D"
                Do
                    prioritymenu()
                    prioritychoice = UCase(Console.ReadLine)
                    Select Case prioritychoice
                        Case "A"
                    End Select
                Loop
        End Select
    Loop Until sorq = "X"
    Console.ReadLine()
End Sub
Sub menu1()
    Console.WriteLine("What program would you like to access? A. Stack B. Queue C. Circular queue D. Priority queue X. Exit")
    Console.WriteLine()
End Sub
Sub stackmenu()
    Console.WriteLine("You are currently in the stack program, what would you like to do? A. Push B. Pop C. Peek D. Output stack E. Check full F. Check empty  X. Exit to main menu")
    Console.WriteLine()
End Sub
Sub queuemenu()
    Console.WriteLine("You are currently in the queue program, what would you like to do? A. Enqueue B. Dequeue C. Output queue D. Check full E. Check empty X. Exit to main menu")
    Console.WriteLine()
End Sub
Sub circularmenu()
    Console.WriteLine("You are currently in the circular queue program, what would you want to do? A. Enqueue B. Dequeue C. Output queue D. Check full E. Check empty X. Exit")
End Sub
Sub prioritymenu()
    Console.WriteLine("You are currently in the priority queue program, what would you like to do? A. Enqueue B. Dequeue C. Output queue D. Check full E. Check empty X. Exit")
    Console.WriteLine()
End Sub
Public Class stack
    Public names(4) As String
    Private nextfreeslot As Integer = 0
    Public Function pop() As String
        Dim popped As String
        If empty() = False Then
            nextfreeslot = nextfreeslot - 1
            popped = names(nextfreeslot)
            Console.WriteLine("Removed from stack: " & popped)
        Else
            Console.WriteLine("No data can be removed")
        End If
        Return popped
    End Function
    Public Function push() As Integer
        If full() = False Then
            Console.WriteLine("Enter the name would you like to add to the stack")
            names(nextfreeslot) = Console.ReadLine
            nextfreeslot += 1
        Else
            Console.WriteLine("No data can be added")
        End If
        Return nextfreeslot
    End Function
    Public Function peek() As Integer
        If empty() = False Then
            Console.WriteLine("The top item in the stack is: " & names(nextfreeslot - 1))
        Else
            Console.WriteLine("There is no item at the top")
        End If
        Return peek
    End Function
    Public Function output() As Integer
        If empty() = True Then
            Console.WriteLine("The stack is empty")
        Else
            Console.WriteLine("Stack contents, from bottom to top: ")
            For x = 0 To 4
                Console.WriteLine(names(x))
            Next
        End If
        Return output
    End Function
    Public Function full() As Boolean
        If nextfreeslot = 5 Then
            Console.WriteLine("Stack is full")
            Return True
        Else
            Console.WriteLine("Stack is not full")
            Return False
        End If
    End Function
    Public Function empty() As Boolean
        If nextfreeslot = 0 Then
            Console.WriteLine("Stack is empty")
            Return True
        Else
            Console.WriteLine("Stack is not empty")
            Return False
        End If
    End Function
End Class
Public Class queue
    Public names(4) As String
    Protected tail As Integer = 0
    Protected head As Integer = 0
    Public Overridable Function dequeue()
        Dim removed As String
        If empty() = False Then
            removed = names(head)
            Console.WriteLine("Removed from queue: " & removed)
            head += 1
            Return removed
        Else
            Console.WriteLine("Cannot dequeue")
            Return False
        End If
    End Function
    Public Overridable Function enqueue() As Integer
        If full() = False Then
            Console.WriteLine("Enter the names you want to add to the queue")
            names(tail) = Console.ReadLine
            tail += 1
        Else
            Console.WriteLine("Cannot enqueue any more names")
        End If
        Return tail
    End Function
    Public Function output() As String
        If empty() = True Then
        Else
            Console.WriteLine("Contents to queue from top to bottom")
            For x = head To (tail - 1)
                Console.WriteLine(names(x))
            Next
        End If
        Return output
    End Function
    Public Function full() As Boolean
        If tail > 4 Then
            Console.WriteLine("Queue is full")
            Return True
        Else
            Console.WriteLine("Queue is not full")
            Return False
        End If
    End Function
    Public Function empty() As Boolean
        If tail = head Then
            Console.WriteLine("Queue is empty")
            Return True
        Else
            Console.WriteLine("Queue is not empty")
            Return False
        End If
    End Function
End Class
Public Class movingQueue
    Inherits queue
    Public Overrides Function dequeue()
        Dim removed As String
        If head = tail Then
            Console.WriteLine("Circular queue is empty")
        ElseIf head = 4 Then
            removed = names(head)
            Console.WriteLine("Removed from circular queue: " & removed)
            head = 0
        Else
            removed = names(tail)
            Console.WriteLine("Removed from circular queue: " & removed)
            head += 1
        End If
        Return MyBase.dequeue
    End Function
    Public Overrides Function enqueue() As Integer
        If tail = 4 Then
            If head > 0 Then
                Console.WriteLine("What would you like to add to the circular queue?")
                names(tail) = Console.ReadLine()
                tail = 0
            End If
        ElseIf tail <> (head - 1) Then
            Console.WriteLine("What would you like to add to the circular queue?")
            names(tail) = Console.ReadLine()
            tail += 1
        Else
            Console.WriteLine("Circular queue is full")
        End If
        Return MyBase.enqueue
    End Function
End Class
Public Class priorityQueue
    Inherits queue
    Private priority As Integer
    Function setPriority()
        Console.WriteLine("Please select a priority for this data item: A. First B. Second C. Third")
        priority = Console.ReadLine()
        Return priority
    End Function
    Public Overrides Function enqueue() As Integer

        Return MyBase.enqueue()
    End Function
End Class

Конечный модуль

1 Ответ

0 голосов
/ 15 марта 2019

Если вы не хотите, чтобы родитель вызывался, тогда не звоните.

Public Overrides Function dequeue()
    Dim removed As String
    If head = tail Then
        Console.WriteLine("Circular queue is empty")
    ElseIf head = 4 Then
        removed = names(head)
        Console.WriteLine("Removed from circular queue: " & removed)
        head = 0
    Else
        removed = names(tail)
        Console.WriteLine("Removed from circular queue: " & removed)
        head += 1
    End If
    Return MyBase.dequeue()  '<---- Don't call MyBase
End Function

Эта строка вызовет родительскую функцию.

Кроме того, включите Option Strict.

Public Overridable Function dequeue() '<---- No return type
    Dim removed As String
    If empty() = False Then
        removed = names(head)
        Console.WriteLine("Removed from queue: " & removed)
        head += 1
        Return removed '<--- Return string
    Else
        Console.WriteLine("Cannot dequeue")
        Return False '<--- Return bool, different return type
    End If
End Function
...