在VB.net中,两个没有参数的只读属性如何互相重载?

发布于 2024-11-06 18:22:57 字数 539 浏览 6 评论 0原文

我注意到一个类可以“重载”其父类的只读属性,即使在类中不允许这样做。我不明白为什么允许这样做或者它能实现什么(如果有的话)。

Class myClass
    ReadOnly Property SomeProp As Integer
        Get
            Return 50
        End Get
    End Property
End Class

Class mySubClass
    Inherits myClass    
    Overloads ReadOnly Property SomeProp As Integer
        Get
            Return 12
        End Get
    End Property
End Class

mySubClass.SomeProp 的签名与 myClass.Prop 相同 - 前者如何重载后者?

实际上,这似乎就像 Shadows 一样起作用,是真的吗?

I've noticed that a class can "overload" a read-only property of its parent class, even though this isn't allowed within a class. I don't understand why this is allowed or what (if anything) it accomplishes.

Class myClass
    ReadOnly Property SomeProp As Integer
        Get
            Return 50
        End Get
    End Property
End Class

Class mySubClass
    Inherits myClass    
    Overloads ReadOnly Property SomeProp As Integer
        Get
            Return 12
        End Get
    End Property
End Class

The signature of mySubClass.SomeProp is identical to myClass.Prop—how can the former overload the latter?

In practice this seems to function just like Shadows, is that true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2024-11-13 18:22:57

本质上是的,通过重载,您将拥有 myClass::SomeProp 和 mySubClass::SomeProp

给定 mySubClass 的实例,对 SomeProp 的调用将解析为 mySubClass::SomeProp 作为最佳匹配。然而,由于它是重载而不是阴影,因此类似的东西

Readonly Property SomeProp(x as Integer) as integer)
  Get
    Return 13
  End Get
End Property

不会编译,因为它缺少重载装饰器。

In essence yes, with the overload you'll have myClass::SomeProp and mySubClass::SomeProp

Given an instance of mySubClass, calls to SomeProp will resolve to mySubClass::SomeProp as the best match. However since it's Overloads and not Shadows, something like

Readonly Property SomeProp(x as Integer) as integer)
  Get
    Return 13
  End Get
End Property

won't compile since it lacks the Overloads decorator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文