0

假设我有一个包含多个方法的类。我想将这些方法组织成可以访问的分组,而无需每次都构造一个新对象。目的是将类的方法分组到逻辑桶中

例如:

Dim myclass as MyCustomClass
myclass.Shipping.Get_List()
myclass.Production.Get_List()

做这个的最好方式是什么?我尝试了嵌套类,但 VB.NET 不允许我访问如上所示的方法。

4

3 回答 3

0

我找到了我正在寻找使用接口的解决方案

Public Interface ICompany
    Function Company_List() As DataTable
End Interface

Public Class MainClass
    Public Company As ICompany = New CompanyClass

    Public Sub New()
        MyBase.New()
    End Sub

    Private Class CompanyClass
        Public Sub New()
            MyBase.New()
        End Sub

        Public Function Company_List() As DataTable
              My code....
        End Function
End Class
End Class
于 2012-06-13T18:49:21.527 回答
0

请注意,您很可能应该重新评估您的架构,您可以像这样实现您的模式:

Public Class MyCustomClass
    Private _shippingList As List(Of String)
    Private _productionList As List(Of String)

    Public Production As ProductionClass
    Public Shipping As ShippingClass

    Public Sub New()
        Production = New ProductionClass(Me)
        Shipping = New ShippingClass(Me)
    End Sub

    Public Class ShippingClass
        Private _owner As MyCustomClass

        Public Sub New(owner As MyCustomClass)
            _owner = owner
        End Sub

        Public Function Get_List()
            Return _owner._productionList
        End Function
    End Class

    Public Class ProductionClass
        Private _owner As MyCustomClass

        Public Sub New(owner As MyCustomClass)
            _owner = owner
        End Sub

        Public Function Get_List()
            Return _owner._productionList
        End Function
    End Class

End Class

但是,如果您的真正意图只是以更易于访问和合乎逻辑的方式组织方法,我建议您考虑:

Public Class MyCustomClass
    Public Sub ShippingListGet()
    End Sub

    Public Sub ShippingListAddTo()
    End Sub

    Public Sub ShippingThatDO()
    End Sub

    Public Sub ShippingThisDo()
    End Sub

    Public Sub ProductionListGet()
    End Sub

    Public Sub ProductionListAddTo()
    End Sub

    Public Sub ProductionThisDo()
    End Sub

    Public Sub ProductionThatDo()
    End Sub
End Class

请记住,有些人认为这很难阅读。我个人更喜欢按照这些思路组织,因此当方法按字母顺序排序时,它们会按逻辑分组。

于 2012-06-13T04:23:53.887 回答
0

所以这就是我会做你想做的事

这不是世界上最好的设计,但它会起作用

我建议您将实际的 get_list 和其他类型的方法/属性移动到特定的类中,同时将公共的保留在父类中,在这种情况下是测试

但是,我不知道你的代码是什么样的,所以从那时起,这是你的选择

Module Module1

    Sub Main()
        Dim test As New test
        test.Production.Get_List()
        test.Shipping.Get_List()
    End Sub

End Module


Public Class Shipping
    Private parent As test

    Public Sub New(ByRef parent As test)
        Me.parent = parent
    End Sub

    Public Function Get_List() As List(Of Integer)
        Return parent.GetShipping_List
    End Function
End Class

Public Class Production
    Private parent As test

    Public Sub New(ByRef parent As test)
        Me.parent = parent
    End Sub

    Public Function Get_List() As List(Of Integer)
        Return parent.GetProduction_List
    End Function
End Class

Public Class test
    Public Property Production As Production
    Public Property Shipping As Shipping


    Public Function GetShipping_List() As List(Of Integer)
        Return Nothing
    End Function

    Public Function GetProduction_List() As List(Of Integer)
        Return Nothing
    End Function

    Public Sub New()
        Production = New Production(Me)
        Shipping = New Shipping(Me)
    End Sub
End Class
于 2012-06-13T03:16:32.030 回答