天天看點

執行個體023公有和私有

Module Module1

    Public Class a

        Private thisX As Single '這是私有的

    End Class

    Public Class b

        Inherits a

        Public Sub Ok()

            Dim x As Integer

            'thisX=10 這是錯誤的

            x = 10

            MsgBox(x)

        End Sub

    End Class

    Public Class 人員

        Private pName As String

        '私有的方法,隻能在人員類使用

        Private Sub Work()

            MsgBox("Working", MsgBoxStyle.OkOnly, pName)

        End Sub

        Public WriteOnly Property Name As String

            Set(ByVal value As String)

                pName = value

            End Set

        End Property

        '公有方法

        Public Sub SpeakOutName()

            MsgBox("My Name is " & pName, MsgBoxStyle.Information, "人員")

        End Sub

        '公有方法

        Public Sub GoToWork()

            MsgBox("On the way ", MsgBoxStyle.OkOnly, pName)

            '調用私有方法

            Work()

        End Sub

    End Class

    Public Class 售貨員

        Inherits 人員

        Public Sub SetName(ByVal MyName As String)

            Name = MyName

        End Sub

    End Class

    Sub Main()

        Dim MrLee As New 售貨員

        MrLee.SetName("李遠")

        MrLee.SpeakOutName()

        MrLee.GoToWork()

    End Sub

End Module