基类唯一方法? [英] Base-Class only Methods?
问题描述
有只有基类的方法只有一种方式?
我有一个工作中使用的模块的周围,但这种分离,这将仅由基类可使用的功能。
我想起线以下
公众为MustInherit级令牌
令牌的东西
NotInheritable共享功能解析(对T令牌)(CR由于CharReader)为T
也将是一件好事,能够做到以下几点,而不诉诸
'到反射基于bodgery。
返回T.Parser(CR)
端功能
末级
公共类数字
继承令牌
数字的东西
受保护的共享功能分析器(CR作为CharReader)作为数字
如果CR.Current.HasValue = false,那么返回任何结果
情况下,选择CR.Value
案0C到9C
返回新的数字(CR.Index,0)
案例否则
返回False
最终选择
端功能
所以现在当
暗淡D0 = Token.Parse(中位数)(CR)
但
暗淡D1 =数字。
不显parse方法。
那么,如何才能做到这一点? (如果可能的话)
修改
目前的实现 这应该真的是一个基类只能在令牌类方法
公共模块TokenModule
公共功能解析(对T令牌)(CR由于CharReader)为T
这是基于讨厌的反射Bodge招聘
' 为什么?我想写什么。 (呼吁通用(constrianed)类型说明符的静态方法。)
'返回T.Parser(CR)
开始Bodgery {
昏暗的TT为T
TT =的GetType(T).InvokeMember(分析器,
Reflection.BindingFlags.InvokeMethod +
Reflection.BindingFlags.NonPublic +
Reflection.BindingFlags.Static,没什么,TT,{CR})
返回TT
'}结束Bodgery
端功能
前端模块
令牌(基础)类
公众为MustInherit级令牌
私人_index作为整数
私人_count作为整数
保护朋友子新(BYVAL指数为整数,算作整数)
_index =指数:_count =计数
结束小组
公共只读属性指数为整数
得到
返回_index
最终获取
高端物业
公共只读属性算作整数
得到
返回_count
最终获取
高端物业
受保护的共享功能分析器(CR作为CharReader)令牌
返回任何结果
端功能
末级
数字类
公共类数字
继承Token.Token
私人小组新(BYVAL指数为整数,算作整数)
MyBase.New(指数计数)
结束小组
受保护的过载共享功能分析器(CR作为CharReader)作为数字
昏暗CRC = cr.Current
如果crc.HasValue = false,那么返回任何结果
选择案例crc.Value
案0C到9C
返回新的数字(cr.Index,1)
案例否则
返回任何结果
最终选择
端功能
末级
这样的关系,真正打破了面向对象的概念。如果父类型提供公共的功能,那么孩子应该还有(想想孩子是父母)。
您希望可以很容易地通过组合实现的一种功能(数字将包含标记的实例和代理任何调用是必要的情况下),而不是继承。
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. 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.
这篇关于基类唯一方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!