有没有一种只有基类的方法?只有基础类方法?
我有一个使用模块的工作,但这分开了只能由基类使用的功能。
我认为在以下
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.
不会显示Parse方法。
那么这怎么做呢? (如果可能的话)
编辑
当前实现 这应该真的是一个基类只能在令牌类
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
方法
Digit 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
这将是什么使用? – 2012-02-17 22:32:58
什么是'CharReader'?我需要知道,以帮助你解决'Parse'问题。 – 2012-02-17 22:46:00
什么CharReader它可以是任何类型并不重要。 – 2012-02-17 23:13:22