gpt4 book ai didi

vb.net - 阴影在函数中使用时表现奇怪

转载 作者:行者123 更新时间:2023-12-01 15:30:40 25 4
gpt4 key购买 nike

我知道 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”。有人知道这是什么原因吗?

最佳答案

你的 F 是一个字符串,所以当你指定索引时,它正在查看字符串的索引,而不是第二个带有整数参数的函数。

“u”是“Func”中的第二个字符,由索引 1 指定。

对于您的示例,您还必须隐藏第二个函数:

Class Derived
Inherits Base

Shadows Function F() As String
Return "-"
End Function

Shadows Function F(ByVal n As Integer) As String
Return "X"
End Function
End Class

关于vb.net - 阴影在函数中使用时表现奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com