기타 [vb.net] 다형성(가상함수)
페이지 정보

본문
Module Module1
    Sub Main()
        Dim objParent As ParentClass = New ChildClass
        objParent.GeneralFunc()
        objParent.OverridAbleFunc()
        Console.WriteLine("")
        Dim winarray(3) As Control
        winarray(0) = New Control(1, 2)
        winarray(1) = New Label(3, 4, "aaa")
        winarray(2) = New Button(1, 2)
        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

 
- 이전글How to use Text-to-Speech within a Visual Basic .NET application 22.12.04
 - 다음글[vb.net] 델리 게이트(Delegate) 선언 방법과 간단한 예제 22.11.30
 
댓글목록
등록된 댓글이 없습니다.



