1

我有两个从基类 BaseParser 继承的 Parser 类。我想将任一类用作另一个 Monitor 类中的参数。Parser 类 CS600 和 TCH600 都有两个属性 RawDataList 和 SummaryDataList。CS600 类的 RawDataList 返回一个 List(of CS600Data);TCH600 RawDataList 返回一个列表(TCH600Data)。SummaryDataList 在每个 Parser 类中返回相似的类。CS600Data 和 TCH600Data 派生自基类 BaseData。BaseParser 也有 RawDataList (List(of BaseData)) 和 SummaryDataList (List(of BaseSummaryData))

Monitor 类有一个私有字段 _thisParser,它可以是上述两个具体解析器中的任何一个。我希望能够在 Monitor 类中调用和使用 _thisParser 的 RawDataList 和 SummaryDataList,但是当我构造具体的 Parser 类时,Visual Studio 注意到 CS600 的 RawDataList 属性不能覆盖 BaseParser 的 RawDataList 属性,因为它们的返回值不同类型。

我认为自从 CS600Data 派生自 BaseData(但也添加了它自己的一些新属性)以来,我可以在使用 BaseData 的任何地方使用 CS600Data。我有什么误解?如何正确构建这些类?

Public MustInherit Class BaseParser

    Protected _rawDataList As List(Of RawGasData.BaseData)
    Public MustOverride ReadOnly Property RawDataList() As List(Of RawGasData.BaseData)

    Protected _summaryDataList As List(Of SummaryGasData.BaseSummaryData)
    Public MustOverride ReadOnly Property SummaryDataList() As List(Of SummaryGasData.BaseSummaryData)

    Public Event GasDataCreated(ByVal sender As Object, ByVal e As EventArgs)

    Public Sub New()
    End Sub

    Public Sub New(ByVal dataFilePath As String, ByVal unit As String)
        Load(dataFilePath, unit)
    End Sub

    Public Sub Load(ByVal dataFilePath As String, ByVal unit As String)
        Dim contents = My.Computer.FileSystem.ReadAllText(dataFilePath)
        ParseRawData(contents)
        GenerateSummaryData(contents, unit)
        RaiseEvent GasDataCreated(Me, EventArgs.Empty)
    End Sub

    Protected MustOverride Sub ParseRawData(ByVal fileContents As String)

    Protected MustOverride Sub GenerateSummaryData(ByVal fileContents As String, ByVal unit As String)

End Class

Public Class CS600Parser
    Inherits BaseParser

    Shadows _rawDataList As List(Of RawGasData.CS600Data)
    'Cannot override base class' RawDataList - differ by their return types
    Public Overrides ReadOnly Property RawDataList() As List(Of RawGasData.CS600Data)
        Get
            Return _rawDataList
        End Get
    End Property

    Shadows _summaryDataList As List(Of SummaryGasData.CS600SummaryData)
    'Cannot override base class' SummaryDataList - differ by their return types
    Public Overrides ReadOnly Property SummaryDataList() As List(Of SummaryGasData.CS600SummaryData)
        Get
            Return _summaryDataList
        End Get
    End Property

    Protected Overrides Sub ParseRawData(ByVal fileContents As String)
        Dim data = From d In fileContents.Trim.Split(CChar(vbCrLf)) _
                   Let fields = d.Split(","c).Select(Function(s) s.Trim) _
                   Select New RawGasData.CS600Data With { _
                        .SampleNumber = fields(0), _
                        .UserID = fields(1), _
                        .CarbonValue = CDbl(fields(2)), _
                        .SulfurValue = CDbl(fields(3))}

        _rawDataList = data.ToList

    End Sub

    Protected Overrides Sub GenerateSummaryData(ByVal fileContents As String, ByVal unit As String)
        Dim groupedData = From d In _rawDataList _
                          Group By d.SampleNumber Into Group _
                          Select New SummaryGasData.CS600SummaryData With { _
                            .DataTimeStamp = Now, _
                            .SampleNumber = SampleNumber, _
                            .UserID = Group.First.UserID, _
                            .CarbonAverage = Group.Select(Function(s) s.CarbonValue).Average, _
                            .CarbonUnit = unit, _
                            .SulfurAverage = Group.Select(Function(s) s.SulfurValue).Average, _
                            .SulfurUnit = unit}

        _summaryDataList = groupedData.ToList

    End Sub

End Class

Public Class TCHParser
    Inherits BaseParser

    Shadows _rawDataList As List(Of RawGasData.TCH600Data)
    'Cannot override base class' RawDataList - differ by their return types
    Public Overrides ReadOnly Property RawDataList() As List(Of RawGasData.TCH600Data)
        Get
            Return _rawDataList
        End Get
    End Property

    Shadows _summaryDataList As List(Of SummaryGasData.TCH600SummaryData)
    'Cannot override base class' SummaryDataList - differ by their return types
    Public Overrides ReadOnly Property SummaryDataList() As List(Of SummaryGasData.TCH600SummaryData)
        Get
            Return _summaryDataList
        End Get
    End Property

    Protected Overrides Sub ParseRawData(ByVal fileContents As String)
        Dim data = From d In fileContents.Trim.Split(CChar(vbCrLf)) _
       Let fields = d.Split(","c).Select(Function(s) s.Trim) _
       Select New RawGasData.TCH600Data With { _
            .SampleNumber = fields(0), _
            .UserID = fields(1), _
            .OxygenValue = CDbl(fields(2)), _
            .NitrogenValue = CDbl(fields(3)), _
            .HydrogenValue = CDbl(fields(4)), _
            .MassUsed = CDbl(fields(5))}

        _rawDataList = data.ToList

    End Sub

    Protected Overrides Sub GenerateSummaryData(ByVal fileContents As String, ByVal unit As String)
        Dim groupedData = From d In _rawDataList _
                          Group By d.SampleNumber Into Group _
                          Select New SummaryGasData.TCH600SummaryData With { _
                            .DataTimeStamp = Now, _
                            .SampleNumber = SampleNumber, _
                            .UserID = Group.First.UserID, _
                            .OxygenAverage = Group.Select(Function(s) s.OxygenValue).Average, _
                            .OxygenUnit = unit, _
                            .NitrogenAverage = Group.Select(Function(s) s.NitrogenValue).Average, _
                            .NitrogenUnit = unit, _
                            .HydrogenAverage = Group.Select(Function(s) s.HydrogenValue).Average, _
                            .HydrogenUnit = unit}

        _summaryDataList = groupedData.ToList
    End Sub


End Class


 Public MustInherit Class BaseData
    Implements IGasData

    Private _massUsed As Double
    Public Property MassUsed() As Double Implements IGasData.MassUsed
        Get
            Return _massUsed
        End Get
        Set(ByVal value As Double)
            _massUsed = value
        End Set
    End Property

    Private _sampleNumber As String
    Public Property SampleNumber() As String Implements IGasData.SampleNumber
        Get
            Return _sampleNumber
        End Get
        Set(ByVal value As String)
            _sampleNumber = value
        End Set
    End Property

    Private _userID As String
    Public Property UserID() As String Implements IGasData.UserID
        Get
            Return _userID
        End Get
        Set(ByVal value As String)
            _userID = value
        End Set
    End Property
End Class

Public Class CS600Data
    Inherits BaseData


    Private _carbonValue As Double
    Public Property CarbonValue() As Double
        Get
            Return _carbonValue
        End Get
        Set(ByVal value As Double)
            _carbonValue = value
        End Set
    End Property


    Private _sulfurValue As Double
    Public Property SulfurValue() As Double
        Get
            Return _sulfurValue
        End Get
        Set(ByVal value As Double)
            _sulfurValue = value
        End Set
    End Property

End Class

Public Class TCH600Data
    Inherits BaseData

    Private _oxygenValue As Double
    Public Property OxygenValue() As Double
        Get
            Return _oxygenValue
        End Get
        Set(ByVal value As Double)
            _oxygenValue = value
        End Set
    End Property


    Private _nitrogenValue As Double
    Public Property NitrogenValue() As Double
        Get
            Return _nitrogenValue
        End Get
        Set(ByVal value As Double)
            _nitrogenValue = value
        End Set
    End Property


    Private _hydrogenValue As Double
    Public Property HydrogenValue() As Double
        Get
            Return _hydrogenValue
        End Get
        Set(ByVal value As Double)
            _hydrogenValue = value
        End Set
    End Property

End Class
4

1 回答 1

0

您重写的列表属性,例如RawDataList,应该是抽象基类的类型(或者更好IGasData),而不是多态工作的派生类。如果您发现列出类型列表BaseDataIGasData导致问题,您可能需要检查您的设计是否符合Liskov 替换原则

编译和运行良好的简化插图(VB.Net 2010):

Sub Main    
    Dim parser As BaseParser = New TCHParser()
    Console.WriteLine(parser)   
End Sub

Public MustInherit Class BaseData    
    Public Property SampleNumber() As String
End Class

Public Class TCH600Data 
    Inherits BaseData

    Public Property OxygenValue() As Double
End Class

Public MustInherit Class BaseParser
    Public MustOverride ReadOnly Property RawDataList As List(of BaseData)
End Class

TCHParser可以很好地覆盖该RawDataList属性。另外,我不确定为什么抽象基类有一个Protected _rawDataList字段。删除它,您不再需要担心在派生类型中隐藏该字段。

Public Class TCHParser 
    Inherits BaseParser

    ' Populate the list just so we can test easily.
    Public Sub New()
        Dim data As New List(Of BaseData)
        data.Add(New TCH600Data With {
            .SampleNumber = "1", _
            .OxygenValue = 1.5d })

        data.Add(New TCH600Data With {
            .SampleNumber = "2", _
            .OxygenValue = 2.5d })

        _rawDataList = data
    End Sub

    Private _rawDataList As List(of BaseData)  
    Public Overrides ReadOnly Property RawDataList As List(of BaseData)
        Get
            Return _rawDataList
        End Get
    End Property
End Class
于 2012-04-10T02:10:05.317 回答