继承和覆盖,vb.net

问题描述 投票:-1回答:1

我有一个继承另一个类的类,在我的父类中有一个可覆盖的函数,在子类中有一个重写函数。这两个函数都具有相同的名称。但是,当我在子类的对象中调用该函数时,父函数和子类中的函数都会在彼此之后运行。我只想要重写的功能(在子类中)运行,我该如何解决这个问题?

父类是'队列',子类是'循环队列'。我想要覆盖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

结束模块

vb.net oop inheritance override
1个回答
0
投票

如果您不想要调用父级,则不要调用它。

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 On。

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
© www.soinside.com 2019 - 2024. All rights reserved.