[기타] [vb.net] 다형성(가상함수)

회원사진
하나를하더라도최선을
2022-11-30 16:27 2,581 0

본문



Module Module1
 
    Sub Main()
 
        Dim objParent As ParentClass = New ChildClass
        objParent.GeneralFunc()
        objParent.OverridAbleFunc()
 
        Console.WriteLine("")
 
        Dim winarray(3As Control
        winarray(0= New Control(12)
        winarray(1= New Label(34"aaa")
        winarray(2= New Button(12)
        For Each c As Control In winarray
            If c Is Nothing Then Continue For
            c.DrawControl()
        Next
 
        Console.ReadLine()
    End Sub
 
 
    Public Class ParentClass
 
        Public Sub GeneralFunc()
            Console.WriteLine("ParentGeneralFunc")
        End Sub
 
        Public Overridable Sub OverridAbleFunc()
            Console.WriteLine("ParentOverridAbleFunc")
        End Sub
 
    End Class
 
    Public Class ChildClass
        Inherits ParentClass
 
        Public Shadows Sub GeneralFunc()
            Console.WriteLine("ChildGeneralFunc")
        End Sub
 
        Public Overrides Sub OverridAbleFunc()
            Console.WriteLine("ChildOverridAbleFunc")
        End Sub
 
    End Class
 
    Public Class Control
        Protected top As Integer
        Protected left As Integer
        Public Sub New(ByVal top As Integer, ByVal left As Integer)
            Me.top = top : Me.left = left
        End Sub
 
        Public Overridable Sub DrawControl()
            Console.WriteLine("Control:Drawing controls as {0}, {1}", top, left)
        End Sub
 
    End Class
 
    Public Class Label
        Inherits Control
        Private text As String
        Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal n As String)
            MyBase.New(top, left)
            Me.text = n
        End Sub
 
        Public Overrides Sub DrawControl()
            MyBase.DrawControl()
            Console.WriteLine("Writing string to the Listbox:{0}", text)
        End Sub
    End Class
 
    Public Class Button
        Inherits Control
 
        Public Sub New(ByVal top As Integer, ByVal left As Integer)
            MyBase.New(top, left)
        End Sub
 
        Public Overrides Sub DrawControl()
            Console.WriteLine("Drawing a button at {0}, {1}", top, left)
        End Sub
    End Class
End Module
 

38cc2b5c3d24491658621bcd6958e52d_1669793181_9591.png
 

댓글목록0

등록된 댓글이 없습니다.