如何创建单个类属性的扩展

时间:2018-04-06 19:47:03

标签: vb.net

我有一个原始的类看起来像这样:

Public Class BaseGeoData
    Property GeoOrigin As String
    Property GeoDestination As String
    Property TravelDistance As Double?
    Property TravelTime As Double?

    Public Sub New()
    End Sub
End Class

Public Class GeoData
 Inherits BaseGeoData
   Public Sub New(geoOrigStr As String, geoDestStr As String)
       GeoOrigin = geoOrigStr
       GeoDestination = geoDestStr
       TravelDistance = 5000      'in meters
       TravelTime = 360           'in minutes
   End Sub
End Class

我希望能够添加2个将返回转换值的扩展名:

TravelDistance.ToMiles()   
TravelTime.ToHours()

当我添加一个Module来扩展类时,它提供了整个类的扩展,其中大多数属性永远不会使用扩展。我如何才能为需要它们的属性提供扩展?

3 个答案:

答案 0 :(得分:1)

介绍自己的"单位"用于测量值

Public MustInherit Class Unit
    Public ReadOnly Property Value As Double

    Public MustOverride ReadOnly Property Name As String

    Public Sub New(value As Double)
        Me.Value = value
    End Sub

    Public Overrides Function ToString() As String
        Return $"{Value} {Name}"
    End Function
End Class

Public Class Meter
    Inherits Unit

    Public Sub New(value As Double)
        MyBase.New(value)
    End Sub

    Public Overrides ReadOnly Property Name As String
        Get
            Return "m"
        End Get
    End Property
End Class

Public Class Mile
    Inherits Unit

    Public Sub New(value As Double)
        MyBase.New(value)
    End Sub

    Public Overrides ReadOnly Property Name As String
        Get
            Return "mi"
        End Get
    End Property
End Class

用于创建单位和转换的扩展方法

Public Module UnitConversions

    <Extension>
    Public Function Meters(value As Integer) As Meter
        Return New Meter(value)
    End Function

    <Extension>
    Public Function Miles(value As Integer) As Mile
        Return New Mile(value)
    End Function

    <Extension>
    Public Function ToMiles(meters As Meter) As Mile
        Dim miles = meters.Value * 0.00062137
        Return New Mile(miles)
    End Function

    <Extension>
    Public Function ToMeters(miles As Mile) As Meter
        Dim meters = miles.Value * 1609.344
        Return New Meter(meters)
    End Function
End Module

然后您可以以更易读的方式使用值

TravelDistance = 5000.Meters() ' meters

' Conversion
geoData.TravelDistance.ToMiles() ' miles

Console.WriteLine(geoData.TravelDistance) ' print 3.10685 mi

答案 1 :(得分:0)

您只能将扩展方法添加到类型(即类)中。

TravelDistance的类型为Double?,因此您必须在Double?中添加一种扩展方法。

请注意,它会使该方法适用于每个Double?,这可能不是您想要的。

答案 2 :(得分:0)

我非常喜欢 Plutonix的分辨率,这与我首先要做的一样。

简单并解决了您的初始问题。

Public Class BaseGeoData

  Property GeoOrigin As String
  Property GeoDestination As String
  Property TravelDistance As Double?
  Property TravelTime As Double?

  Public Sub New()
  End Sub

End Class


Public Class GeoData
  Inherits BaseGeoData

  Public Sub New(geoOrigStr As String, geoDestStr As String)
    GeoOrigin = geoOrigStr
    GeoDestination = geoDestStr
    TravelDistance = 5000      'in meters
    TravelTime = 360           'in minutes
  End Sub

  Function DistanceMiles() As Double
    DistanceMiles = (TravelDistance/1609.344)
  End Function

  Function TimeHours() As Double
    DistanceMiles = (TravelTime /60)
  End Function

End Class