实例026 类的重写

    子类定义的属性或方法有时必须要适应特殊的要求,即必须满足特定的功能,因此,子类必须覆盖定义基类中的同名属性或方法。相反情况下。有时基类中定义的属性和方法在子类中将继承发挥作用,因此,基类中的这些属性和方法不能被子类覆盖。

Module Module1
    Public MustInherit Class 人员
        Protected MyName As String
        Private MyAge As Byte
        Public MustOverride Property Action As String
        Public Overridable Property Name As String
            Get
                Return MyName
            End Get
            Set(ByVal value As String)
                MyName = value
            End Set
        End Property

        Public Property Age As Byte
            Get
                Return MyAge
            End Get
            Set(ByVal value As Byte)
                MyAge = value
            End Set
        End Property
    End Class

    Public Class 顾客
        Inherits 人员

        Private MyAction As String
        Public Overrides Property Action As String
            Get
                Return MyAction
            End Get
            Set(ByVal value As String)
                MyAction = value
                MsgBox(MyAction, MsgBoxStyle.Exclamation, Name)
            End Set
        End Property

        Public Overrides Property Name As String
            Get
                Return MyBase.Name
            End Get
            Set(ByVal value As String)
                MsgBox("Hello" & value, MsgBoxStyle.Question, "顾客")
                MyName = value
            End Set
        End Property
    End Class
    Sub Main()
        Dim MrJames As New 顾客
        MrJames.Name = "James"
        MrJames.Action = "Working"
        MrJames.Age = 31
        MsgBox(MrJames.Age)
    End Sub

End Module
 

发布了146 篇原创文章 · 获赞 0 · 访问量 2752

猜你喜欢

转载自blog.csdn.net/ngbshzhn/article/details/105553049