除了在子类中覆盖定义基类中的可覆盖属性或方法外,还可以在子类中使用Shadows关键字来隐藏基类中已经定义过并被子类继承下来的各种字段、属性或方法,并在子类中重新定义同名的字段、属性或者方法。属性或方法隐藏形式如下:
Public Shadows Property 属性名 As 数据类型
Public Shadows Sub 过程名
Public Shadows Function 函数名(参数)As数据类型
采用Shadow隐藏基类同名属性或方法,灵活性较强,不仅允许子类重新定义基类的同名属性或方法,还允许子类将基类属性名作为子类方法名,或将基类的字段名作为属性、方法的名称。
Module Module1
Public Class 人员
Protected MyName As String
Private MyAge As Byte
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
MsgBox(MyAge, MsgBoxStyle.Information, "人员")
End Set
End Property
End Class
Public Class 售货员
Inherits 人员
Public Shadows Sub Name()
If MyName = InputBox("请输入售货员的姓名", "售货员") Then
End If
MsgBox(MyName, MsgBoxStyle.Exclamation, "售货员")
End Sub
Public MyAge As String = "35"
Public Shadows age As Boolean
End Class
Sub Main()
Dim MrZhao As New 人员
Dim MrZhang As New 售货员
Dim str As String
MrZhao.Age = 20
MrZhao.Name = "ZhaoQ"
str = MrZhao.Name
MsgBox(str)
MrZhang.age = True
MrZhang.Name()
str = MrZhang.MyAge
MsgBox(str)
End Sub
End Module