【问题标题】:Implemented property error VB实现属性错误 VB
【发布时间】:2015-08-01 23:46:53
【问题描述】:

我正在使用从接口实现的类。 我想从接口实现一个属性,但出现错误:

类“ListManager”必须为接口“IListManager(Of T)”实现“Property Count As Integer”。实现属性必须具有匹配的“ReadOnly”或“WriteOnly”说明符。

界面:

Namespace Assign_1

    Public Interface IListManager(Of T)

        ''' <summary>
        ''' Return the number of items in the collection m_list
        ''' </summary>
        Property Count As Integer
End Namespace

类:

Namespace Assign_1
    Public Class ListManager(Of T)
        Implements IListManager(Of T)

        Protected m_list As List(Of T)

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New()
            MyBase.New()
            m_list = New List(Of T)
        End Sub

        ''' <summary>
        ''' Property to count the list
        ''' </summary>
        Public ReadOnly Property Count As Integer
            Get
                Return m_list.Count
            End Get
        End Property
End Namespace

有人知道这个问题吗?

【问题讨论】:

  • 要么在接口定义中设为 ReadOnly,在类中删除 readonly,或者更好的是,让它成为一个函数
  • 我尝试在接口定义中将其设为 ReadOnly,删除类中的只读。它没有用。我不明白您所说的“全方位制作功能”是什么意思

标签: vb.net


【解决方案1】:

没有必要将接口定义为泛型,因为接口只是建立了实现哪些属性和方法的协定。

Public Interface IListManager
    ReadOnly Property Count As Int32

    ' function version
    Function GetCount() As Integer
End Interface

Public Class ListMgr(Of T)
    Implements IListManager

    Private myList As List(Of T)

    Public ReadOnly Property Count As Integer Implements IListManager.Count
        Get
            Return myList.Count
        End Get
    End Property

    Public Function GetCount() As Integer Implements IListManager.GetCount
        Return myList.Count
    End Function

End Class

如果您确实需要通用接口,只需进行一点小改动:

Public ReadOnly Property Count As Integer Implements IListManager(Of T).Count

在你输入Implements IMyInterface之后,VS会在你回车时添加样板属性和方法代码。

【讨论】:

    【解决方案2】:

    这对我有用

    Public Interface IListManager(Of T)
        ReadOnly Property count As Integer
    End Interface
    
    Public Class ListManager(Of T) : Implements IListManager(Of TabAlignment)
        Protected m_list As List(Of T)
    
        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New()
            MyBase.New()
            m_list = New List(Of T)
        End Sub
    
        Public ReadOnly Property count As Integer Implements IListManager(Of TabAlignment).count
            Get
                Return Me.m_list.Count
            End Get
        End Property
    End Class
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2017-12-08
      • 2021-07-29
      相关资源
      最近更新 更多