天天看点

实例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