我真的在努力遵循DRY原则。我有一个子程序看起来像这样:
Private Sub DoSupplyModel
OutputLine("ITEM SUMMARIES")
Dim ItemSumms As New SupplyModel.ItemSummaries(_currentSupplyModel, _excelRows)
ItemSumms.FillRows()
OutputLine("")
OutputLine("NUMBERED INVENTORIES")
Dim numInvs As New SupplyModel.NumberedInventories(_currentSupplyModel, _excelRows)
numInvs.FillRows()
OutputLine("")
End Sub
我希望使用泛型将它们合并为一个方法。值得注意的是,ItemSummaries和NumberedInventories都派生自同一基类DataBuilderBase。
我无法找出允许我在该方法中执行ItemSumms.FillRows和numInvs.FillRows的语法。
FillRows在基类中声明为Public Overridable Sub。
提前感谢您的帮助。
编辑: 这是我的最终结果。
Private Sub DoSupplyModels()
DoSupplyModelType("ITEM SUMMARIES",New DataBlocks(_currentSupplyModel,_excelRows)
DoSupplyModelType("DATA BLOCKS",New DataBlocks(_currentSupplyModel,_excelRows)
End Sub
Private Sub DoSupplyModelType(ByVal outputDescription As String, ByVal type As DataBuilderBase)
OutputLine(outputDescription)
type.FillRows()
OutputLine("")
End Sub
但是回答自己的问题...我本可以这样做...
Private Sub DoSupplyModels()
DoSupplyModelType(Of Projections)("ITEM SUMMARIES")
DoSupplyModelType(Of DataBlocks)("DATA BLOCKS")
End Sub
Private Sub DoSupplyModelType(Of T as DataBuilderBase)(ByVal outputDescription As String, ByVal type As T)
OutputLine(outputDescription)
Dim type as New DataBuilderBase (_currentSupplyModel,_excelRows)
type.FillRows()
OutputLine("")
End Sub
这样对吗?
Seth
ItemSummaries
或NumberedInventories
吗?如果是,那么你可以简单地使用基类DataBuilderBase
作为参数类型进行接收。在这种情况下,你不需要使用泛型。 - Jorge Ferreira