[英]Base-Class only Methods?
Is there a way of only-base class only methods? 有没有一种只有基类的方法?
I have a work around of using a module, but this separates the functionality which will only be utilized by the base-class. 我有一个使用模块的解决方法,但这将仅由基类使用的功能分开了。
I was think on the line of the following 我想到了以下几点
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
So now when 所以现在
Dim d0 = Token.Parse(Of Digit)(cr)
but 但
Dim d1 = Digit.
wouldn't show the Parse Method. 不会显示解析方法。
So how can this be done? 那么怎么做呢? (If possible at all) (如果可能的话)
EDIT 编辑
Current Implementations This should really a Base Class only method in the Token Class 当前实现这实际上应该是令牌类中的仅基类方法
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
Token (Base) Class 令牌(基础)类
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
That kind of relationship really breaks the OO Concepts. 这种关系真的打破了OO概念。 If a parent type provides public functionality, then the child should as well (think Child is a Parent). 如果父类型提供公共功能,则子对象也应该(认为子对象是父)。
The kind of functionality you want can easily be achieved via composition (Digit would contain an instance of Token and proxy whatever calls are necessary to that instance) rather than inheritance. 您想要的功能类型可以通过组合轻松实现(Digit将包含令牌实例和代理该实例所需的任何调用)而不是继承。
wouldn't show the Parse Method. 不会显示解析方法。
Not sure I follow but you only seem concerned about IntelliSense showing the method. 不确定我是否遵循,但是您似乎只担心IntelliSense显示该方法。 That's easy to solve with EditorBrowsableAttribute: 使用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
If CharReader
implements IConvertible
, you can convert it to other types with System.Convert
. 如果CharReader
实现IConvertible
,您可以使用System.Convert
将其转换为其他类型。 The standard types like Boolean
, String
, Char
, Int32
, Double
, etc. implement IConvertible
. Boolean
, String
, Char
, Int32
, Double
等标准类型实现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
can be hidden by shadowing it with a non public method 可以通过使用非公共方法对其进行遮蔽来隐藏Parse
Protected Shared Shadows Function Parse(ByVal CR As CharReader) As Digit
^
|
(note the Shadows keyword)
Note that this works only for shared methods, since you access them through the type name and not through an instance. 请注意,这仅适用于共享方法,因为您通过类型名称而不是通过实例访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.