본문 바로가기
카테고리 없음

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

by IT HUB 2022. 11. 30.
728x90
반응형
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

 

반응형

댓글