加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在VB.net中重载与重写

发布时间:2020-12-17 00:28:22 所属栏目:大数据 来源:网络整理
导读:Other1和Other2 Class的上一个属性的行为差异是什么? 注意,除了返回类型的ovrloaded的Other2的上一个属性,因为bean更改为Other2,而它保留为Other1的Base. Public Class Base Private _Previous as Base Protected Overridable ReadOnly Property Previous A
Other1和Other2 Class的上一个属性的行为差异是什么?

注意,除了返回类型的ovrloaded的Other2的上一个属性,因为bean更改为Other2,而它保留为Other1的Base.

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读