gpt4 book ai didi

vb.net - VB.net 中的重载与覆盖

转载 作者:行者123 更新时间:2023-12-04 01:45:16 24 4
gpt4 key购买 nike

Previous 的行为差异是什么? Other1的属性和 Other2类。

请注意重载 Previous 的返回类型Other2的属性已更改为 Other2而它保持为 BaseOther1 .

Public Class Base
Private _Previous as Base

Protected Overridable ReadOnly Property Previous As Base
Get
Return _Previous
End Get
End Property

Public Sub New(Previous as Base)
_Previous = Previous
End Sub
End Class

Public Class Other1
Inherits Base
Private _Parent as SomeType

Protected Overrides ReadOnly Property Previous As Base
Get
Return _Parent.Previous.Something
End Get
End Property

Public Sub New(Parent as SomeType)
MyBase.New(Nothing)
_Parent = Parent
End Sub
End Class

Public Class Other2
Inherits Base
Private _Parent as SomeType

Protected Overloads ReadOnly Property Previous As Other2
Get
Return _Parent.Previous.Something
End Get
End Property

Public Sub New(Parent as SomeType)
MyBase.New(Nothing)
_Parent = Parent
End Sub
End Class

最佳答案

在我对 Jim Wooley's answer 的评论之一之后, “它看起来像是对重载的属性进行了遮蔽。”我在 this article 看到了光.

因此,Other2 类中的重载 表现得更像阴影而不是覆盖 .有一个comments在这篇特别有启发性的文章中:

The confusion arises because the keyword "Overloads" isn't what a C# programmer considers an overload in the traditional OO sense. It's a type of hiding that is specific to VB.Net. You can actually swap the keyword SHADOWS with OVERLOADS in most cases, and the behavior is the same. The difference is when you have a base class with multiple overloaded method signatures. If you declare a method in a subclass with a matching name, and the SHADOWS keyword, it will hide EVERY overload of that method in the base class. If you use the OVERLOADS keyword instead, it will only hide the base class method with an identical signature.

关于vb.net - VB.net 中的重载与覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7877378/

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