【问题标题】:Implement properties in VB.NET在 VB.NET 中实现属性
【发布时间】:2012-08-17 13:03:35
【问题描述】:

我有一个类MyVisible 有一个属性Visible

我有一个类MySuperVisible 实现了一个接口ISuperVisible,它还包含属性Visible

我应该如何在“MySuperVisible”类中实现接口“ISuperVisible”的“Visible”属性?

Class MySuperVisible
  Inherits MyVisible
  Implements ISuperVisible

... 

Private Property PrivatePropertyPlaceholder Implements ISuperVisible.Visible
  Get
    Return MyBase.Visible
  End Get
  Set
    MyBase.Visible = value
  End Set
End Property

实现私有属性是唯一的解决方案吗?

附言。 MySuperVisible 继承了 MyVisible,所以我需要实现一个已经存在于基类中的属性。

【问题讨论】:

  • 为什么要实现私有属性? MyBase.Visible 是什么?我想你还没有给我们所有的信息。难道MySuperVisible继承自MyVisible并实现ISuperVisible
  • 如果我理解正确,MyVisible 中的Visible 属性与实现ISuperVisible 中的Visible 的需要相冲突。是对的吗?如果是这样,我相信 Mike C 是对的:如果我没记错的话,Shadows 应该强制 MySuperVisible 中的实现覆盖 MyVisible 中的实现。
  • 是的,感谢 Mike C 完成了这个问题。 MySuperVisible继承了MyVisible,所以我需要实现一个已经存在于基类中的属性...

标签: .net vb.net


【解决方案1】:

有趣的问题。我认为Shadows 关键字可能是去这里的合适方式。这使得事情比PrivatePropertyPlaceholder 更明确一点:

Public Shadows Property Visible As Boolean Implements ISuperVisible.Visible
    Get
        Return MyBase.Visible
    End Get
    Set(value As Boolean)
        MyBase.Visible = value
    End Set
End Property

【讨论】:

  • Public Shadows Property Visible As Boolean Implements ISuperVisible.Visible 这一行怎么样?需要我明确地写出所有的正文吗?
  • 如果在没有属性主体及其显式设置/获取自/到 mybase 的情况下对其进行遮蔽,则不会设置基类可见属性。
猜你喜欢
  • 2014-03-05
  • 2011-03-05
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多