VB.NET 中的阴影与重载
当我们在 C# 中使用 new
时,我个人认为这只是重写没有虚拟/可重写声明的属性的一种解决方法,在 VB.NET 中我们有两个“概念”Shadows
和重载
。
在哪种情况下更喜欢其中一种?
When we have new
in C#, that personally I see only as a workaround to override a property that does not have a virtual/overridable declaration, in VB.NET we have two "concepts" Shadows
and Overloads
.
In which case prefer one to another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,我已经通过使用
Shadows
与Overloads
编译相同的代码来确认基类中具有相同名称和签名的方法,并查看 ildasm 的输出两者都适用。唯一的区别是 Overloads 情况指定了 hidebysig。Jon Skeet 在这个答案中最好地解释了这一点的重要性。
但简单来说,这意味着只有当基类重载了正在重新定义的方法时,才会有真正的区别:
Shadows
将导致所有这些重载无法通过以下方式调用
派生类,其中
Overloads
仅替换 one 方法。请注意,这只是一种语言构造,并非由 CLI 强制执行(即 C# 和 VB.NET 强制执行此操作,但其他语言可能不会)。
一个简单的代码示例:
上面的输出:
输出显示直接调用
C2
时使用Shadows
调用,而不是通过C1
间接调用时使用Shadows
调用代码>.I have actually confirmed by compiling the same code with
Shadows
vsOverloads
for a method with an identical name and signature in the base class and looking at the output fromildasm
for both. The only difference is theOverloads
case specifieshidebysig
.The significance of this is best explained by Jon Skeet in this answer.
But simply it means there is only a real difference if the base class has overloads of the method being redefined:
Shadows
will cause all of thoseoverloads to be uncallable through
the derived class, where as
Overloads
only replaces the one method.Note that this is only a language construct and not enforced by the CLI (i.e. C# and VB.NET enforce this but other languages may not).
A simple code example:
The output of the above:
The output shows the
Shadows
calls are used whenC2
is called directly and not when called indirectly throughC1
.存在三个密切相关的概念;覆盖、遮蔽和超载。
重写是指为虚拟方法创建新的实现。
影子是指为方法创建新的非虚拟实现。
重载是指添加具有相同名称但参数不同的方法。
所有三个概念在 C# 和 VB 中都可用。
There are three closely related concepts; overriding, shadowing and overloading.
Overriding is when you make a new implementation for a virtual method.
Shadowing is when you make a new non-virtual implementation for a method.
Overloading is when you add a method with the same name but different parameters.
All three concepts are available both in C# and VB.
Microsoft 文档指出:
因此,结果是相同的:子成员替换基本成员。然而,您希望实现这种结果的原因通常分为两类:
现在考虑到在定义影子成员时影子成员通常不存在,编译器默认情况下假设
影子
的原因就变得显而易见了。引用此 微软文章也值得一读。
The Microsoft documentation indicates:
Hence, the result is the same: the child member replaces the base member. However the reasons why you may wish to achieve such result typically fall under two categories:
Having now in mind that a shadowed member typically does not exist at the time the shadowing member is defined, the reasons why by default the compiler will assume
Shadows
become obvious.The parts referring to the
Shadows
keyword of this Microsoft article are also worth reading.Shadows
适用于基类为Function SomeMethod() As String
并且您希望使用Function SomeMethod() As Integer
的情况。基本上,改变返回类型。Overloads
适用于基类为Function SomeMethod() As String
并且您想要添加参数,例如Function SomeMethod(ByVal value As Integer) As字符串
。Shadows
is for cases where your base class isFunction SomeMethod() As String
and you want to haveFunction SomeMethod() As Integer
. Basically, to change the return type.Overloads
is for case where your base class isFunction SomeMethod() As String
and you want to add a parameter such asFunction SomeMethod(ByVal value As Integer) As String
.