仅基类?方法?

时间:2012-02-17 22:31:17

标签: .net vb.net inheritance

有没有办法只使用基础类方法?

我有一个使用模块的工作,但这将分离仅由基类使用的功能。

我想到了以下

Public MustInherit Class Token
  ' Token stuff
  NotInheritable Shared Function Parse(Of T As Token)(CR As CharReader) As T
    ' Would also be good to be able to do the following without resorting
    ' to the reflection based bodgery.
    Return T.Parser(CR)
  End Function
End Class

Public Class Digit
  Inherit Token
  ' Digit Stuff
  Protected Shared Function Parser(CR As CharReader) As Digit
    If CR.Current.HasValue = False Then Return Nothing
      Case Select CR.Value
        Case "0"c To "9"c
          Return New Digit(CR.Index,0)
      Case Else
        Return False
      End Select
  End Function

所以现在

Dim d0 = Token.Parse(Of Digit)(cr)

但是

Dim d1 = Digit.

不会显示解析方法。

那怎么办呢? (如果可能的话)

修改

当前实施 这应该是Token Class中唯一的Base类方法

Public Module TokenModule
  Public Function Parse(Of T As Token)(cr As CharReader) As T
  '  
  ' Here Be Nasty Reflection Based Bodge Job 
  '
  ' Why? What I want to write. ( Call a static method on the generic (constrianed) type     specifier.)
  '
  ' Return T.Parser(cr)
  ' 
  ' Start Bodgery {
  Dim tt As T
  tt = GetType(T).InvokeMember("Parser",
                                Reflection.BindingFlags.InvokeMethod +
                                Reflection.BindingFlags.NonPublic +
                                Reflection.BindingFlags.Static, Nothing, tt, {cr})
  Return tt
  ' } End Bodgery
End Function
End Module

令牌(基础)类

Public MustInherit Class Token
  Private _Index As Integer
  Private _Count As Integer
  Protected Friend Sub New(ByVal Index As Integer, Count As Integer)
    _Index = Index : _Count = Count
  End Sub
  Public ReadOnly Property Index As Integer
    Get
      Return _Index
    End Get
  End Property
  Public ReadOnly Property Count As Integer
    Get
      Return _Count
    End Get
  End Property
  Protected Shared Function Parser(cr As CharReader) As Token
    Return Nothing
  End Function
End Class

数字课程

Public Class Digit
  Inherits Token.Token
  Private Sub New(ByVal Index As Integer, Count As Integer)
    MyBase.New(Index, Count)
  End Sub
  Protected Overloads Shared Function Parser(cr As CharReader) As Digit
    Dim crc = cr.Current
    If crc.HasValue = False Then Return Nothing 
      Select Case crc.Value
        Case "0"c To "9"c
          Return New Digit(cr.Index, 1)
        Case Else
          Return Nothing
        End Select
  End Function
End Class

3 个答案:

答案 0 :(得分:2)

这种关系真的打破了OO Concepts。如果父类型提供公共功能,那么孩子也应该(认为Child是父母)。

您想要的功能可以通过合成轻松实现(数字将包含令牌实例和代理该实例所需的任何调用)而不是继承。

答案 1 :(得分:1)

  

不会显示解析方法。

不确定我是否关注,但您似乎只关心IntelliSense显示该方法。使用EditorBrowsableAttribute可以很容易地解决这个问题:

Imports System.ComponentModel
...
Class Token
    Public Shared Sub Parse()
        '' etc
    End Sub
End Class

Class Digit
    Inherits Token

    <EditorBrowsable(EditorBrowsableState.Never)> _
    Public Shared Shadows Sub Parse()
        Token.Parse()
    End Sub
End Class

答案 2 :(得分:0)

如果CharReader实施IConvertible,您可以将其转换为System.Convert的其他类型。 BooleanStringCharInt32Double等标准类型实施IConvertible

Shared Function Parse(Of T As Token)(ByVal CR As CharReader) As T
    Return DirectCast(Convert.ChangeType(CR, GetType(T)), T)
End Function

-

Parse可以通过使用非公开方法隐藏它来隐藏

Protected Shared Shadows Function Parse(ByVal CR As CharReader) As Digit
                    ^
                    |
            (note the Shadows keyword)

请注意,这仅适用于共享方法,因为您通过类型名称而不是通过实例访问它们。