使用具有不同返回类型的多态的子类

时间:2012-04-10 00:54:33

标签: vb.net polymorphism

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

Monitor类有一个私有字段_thisParser,可以是上面两个具体的Parser。我希望能够在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

1 个答案:

答案 0 :(得分:1)

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

编译并运行得很好的简化插图(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