具有不同签名的相似类
我有两个课程:
Public Class Subscribing
Private _subscribingObjects As IList(Of String)
Public Sub Add(ByVal obj As SubscribeObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Public Class Providing
Private _providingObjects As IList(Of String)
Public Sub Add(ByVal obj As ProvideObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
是否有更优雅的方法来添加此操作? 一个类就足够了,但由于 Add 方法有不同的参数,所以一个类确实行不通。
任何帮助,将不胜感激。
I have two classes:
Public Class Subscribing
Private _subscribingObjects As IList(Of String)
Public Sub Add(ByVal obj As SubscribeObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Public Class Providing
Private _providingObjects As IList(Of String)
Public Sub Add(ByVal obj As ProvideObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Is there a more elegant way to add do this? One class would suffice, but since the Add methods have different arguments, then one really wouldn't work.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这?
this?
你的添加功能应该没问题。 只要传入不同的变量类型,就可以使函数名称相同。 同一类中不允许您删除 Subs,因为它使用相同的参数 Integer。
Your add functions should be fine. As long as you have different variable types being passed in you can have the function names be the same. Your remove Subs will not be allowed in the same class because it is using the same parameter Integer.
呃..可能不是。 它们差异很大,以至于您甚至无法与它们交互。
Eh.. probably not. They are different enough that you cant even Interface them.
我个人不会将两种职责(订阅和提供)混合在一个类中。 只需从 List(Of T) 继承即可轻松简化类本身
如果您确实想了解一个类并确保它只能接受 SubscribeObject 和 ProvideObject , > 分别在 SubscribeObject 和 ProvideObject 中实现通用接口。 然后创建一个接受该接口的泛型类:
I personally wouldn't mix the two responsibilities (of subscribing and providing) in one class. The classes themselves can easily be simplified by just inheriting from List(Of T)
If you really want to get down to one class and make sure that it can only accept SubscribeObject and ProvideObject respectively, implement a common interface in both SubscribeObject and ProvideObject. Then create a generic class that accepts the interface: