具有不同签名的相似类

发布于 2024-08-01 18:12:24 字数 668 浏览 3 评论 0原文

我有两个课程:


    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不交电费瞎发啥光 2024-08-08 18:12:24

这?

Public Class SubscribingProviding(Of t)

Private _subscribingObjects As IList(Of String)

Public Sub Add(ByVal obj As t)
    '...code...'
End Sub

Public Sub Remove(ByVal index As Integer)
    '...code...'
End Sub

End Class

this?

Public Class SubscribingProviding(Of t)

Private _subscribingObjects As IList(Of String)

Public Sub Add(ByVal obj As t)
    '...code...'
End Sub

Public Sub Remove(ByVal index As Integer)
    '...code...'
End Sub

End Class
云裳 2024-08-08 18:12:24

你的添加功能应该没问题。 只要传入不同的变量类型,就可以使函数名称相同。 同一类中不允许您删除 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.

沫雨熙 2024-08-08 18:12:24

呃..可能不是。 它们差异很大,以至于您甚至无法与它们交互。

Eh.. probably not. They are different enough that you cant even Interface them.

瀟灑尐姊 2024-08-08 18:12:24

我个人不会将两种职责(订阅和提供)混合在一个类中。 只需从 List(Of T) 继承即可轻松简化类本身

Public Class Subscribing
    Inherits List(Of SubscribeObject)
End Class

Public Class Providing
    Inherits List(Of ProvideObject)
End Class

如果您确实想了解一个类并确保它只能接受 SubscribeObjectProvideObject , > 分别在 SubscribeObjectProvideObject 中实现通用接口。 然后创建一个接受该接口的泛型类:

' Common interface '
Public Interface ISubscribeProvideObject
End Interface

' SubscribeObject and ProvideObject both implementing the common interface '
Public Class SubscribeObject
    Implements ISubscribeProvideObject
    '...'
End Class

Public Class ProvideObject
    Implements ISubscribeProvideObject
    '...'
End Class

' Generic class accepting both types '
Public Class SubscribingProviding(Of T As ISubscribeProvideObject)
    Inherits List(Of T)
    '... Add() and Remove() methods only needed if custom logic applies ...'
End Class

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)

Public Class Subscribing
    Inherits List(Of SubscribeObject)
End Class

Public Class Providing
    Inherits List(Of ProvideObject)
End Class

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:

' Common interface '
Public Interface ISubscribeProvideObject
End Interface

' SubscribeObject and ProvideObject both implementing the common interface '
Public Class SubscribeObject
    Implements ISubscribeProvideObject
    '...'
End Class

Public Class ProvideObject
    Implements ISubscribeProvideObject
    '...'
End Class

' Generic class accepting both types '
Public Class SubscribingProviding(Of T As ISubscribeProvideObject)
    Inherits List(Of T)
    '... Add() and Remove() methods only needed if custom logic applies ...'
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文