避免重复使用不同类的相同共享成员

时间:2017-08-13 17:59:52

标签: .net vb.net inheritance shared

考虑以下两个类:

Class A
    Shared Property P1 As Integer = 1
    Shared Property P2 As Integer = 5
    '
    '
    '
    Shared Property P20 As Integer = 123

    Shared Property A1 As Integer
End Class
Class B
    Shared Property P1 As Integer = 2
    Shared Property P2 As Integer = 7
    '
    '
    '
    Shared Property P20 As Integer = 456

    Shared Property B1 As Integer
End Class

显然,P1P20会在多个类中重复,但它们具有不同的值。对于非共享成员,为避免这种情况,我们通常会创建一个包含所有公共成员的基类,然后派生该类。

那么,是否有类似的方法可以避免共享成员的重复?

编辑:

好的,我实际上要做的是以下几点:
除了其他列之外,我有多个共享某些列的Excel工作表(列顺序不一样)。我有一个方法对表单进行一些操作,所以我开始这样做:

UpdateSheet(productsDT, filePath, New Integer() {1, 8, 9, 16, 17, 29, 30})

Private Sub UpdateSheet(productsDT As DataTable, sheetPath As String, columnIndices As Integer())
    Dim codeColIndex As Integer = columnIndices(0)
    Dim stockColIndex As Integer = columnIndices(1)
    Dim newStockColIndex As Integer = columnIndices(2)
    Dim priceColIndex As Integer = columnIndices(3)
    Dim newPriceColIndex As Integer = columnIndices(4)
    Dim newItemColIndex As Integer = columnIndices(5)
    Dim removedItemColIndex As Integer = columnIndices(6)

    'Excel work
    '
    '
    xlSheet.Cells(rowIndex, codeColIndex) = '....
End Sub

但是这个方法(使用数组)的问题是共享列的数量可能会变大,而且我也想访问其他列的索引,所以我决定更改{{1方法类似于以下内容:

UpdateSheet

或者使用枚举变量而不是Private Sub UpdateSheet(productsDT As DataTable, sheetPath As String, columnIndices As MyIndicesClass) Dim codeColIndex As Integer = MyIndicesClass.Code ' ' End Sub 参数来确定传递的工作表的类型。

希望更有意义,我愿意接受任何建议。

1 个答案:

答案 0 :(得分:1)

您可以使用Factory Pattern创建更新程序类。这些类有一个共同的基类。每个派生类都将在其构造函数中设置列索引。正如评论中所提到的,Enum可用于告诉工厂实例化哪个类。

基类

MustInherit Class SheetUpdaterBase
    Protected Property CodeColIndex As Integer
    Protected Property StockColIndex As Integer
    Public Sub UpdateSheet(productsDT As DataTable, sheetPath As String)
        ' Excel work
    End Sub
End Class

派生类

Class UpdaterClass1
    Inherits SheetUpdaterBase
    Public Sub New()
        CodeColIndex = 1
        StockColIndex = 2
    End Sub
End Class

Class UpdaterClass2
    Inherits SheetUpdaterBase
    Public Sub New()
        CodeColIndex = 1
        StockColIndex = 5
    End Sub
End Class

枚举

Enum UpdaterClassEnum
    UpdaterClass1
    UpdaterClass2
End Enum

班级创作者(工厂)

Module Factory
    Public Function CreateUpdater(updaterClass As UpdaterClassEnum) As SheetUpdaterBase
        Select Case updaterClass
            Case UpdaterClassEnum.UpdaterClass1
                Return New UpdaterClass1()
            Case UpdaterClassEnum.UpdaterClass2
                Return New UpdaterClass2()
            Case Else
                Return Nothing
        End Select
    End Function
End Module

用法

Module Module1
    Sub Main()
        Dim myUpdater1 = Factory.CreateUpdater(UpdaterClassEnum.UpdaterClass1)
        myUpdater1.UpdateSheet(New DataTable(), "path1")
        Dim myUpdater2 = Factory.CreateUpdater(UpdaterClassEnum.UpdaterClass1)
        myUpdater2.UpdateSheet(New DataTable(), "path2")
    End Sub
End Module

请参阅Wikipedia上Factory method pattern的条目;除了使用接口外,它与此代码基本相同。我选择了一个抽象类来定义UpdateSheet,一旦在构造函数中设置了列索引,就可以定义公共代码。