我知道 VB.net 在谈论阴影和重载时很奇怪,但这让我完全困惑。
我正在使用类似于以下模型的模型。父类:
Public Class Base
Function F() As String
Return "F() in Base Class"
End Function
Function F(ByVal n As Integer) As String
Return "F(" + n.ToString() + ") in Base Class"
End Function
End Class
还有这个:
Class Derived
Inherits Base
Shadows Function F() As String
Return "-"
End Function
End Class
运行以下命令时:
Sub Main()
Dim parent As Base = New Base()
Dim child As Derived = New Derived()
Console.WriteLine(parent.F())
Console.WriteLine(parent.F(1))
Console.WriteLine("------------")
Console.WriteLine(child.F())
Console.WriteLine(child.F(1)) 'this should not compile, due to the shadow keyword.
Console.Read()
End Sub
抛出 IndexOutOfRangeException。此外,当更改(在派生类中)时:返回“-”以返回“派生类中的函数”控制台打印字符“u”。有人知道这是什么原因吗?