gpt4 book ai didi

.net - 测试该泛型类型实现一个接口(interface)并使用该接口(interface)

转载 作者:行者123 更新时间:2023-12-04 04:38:46 26 4
gpt4 key购买 nike

我正在构建一个使用物理特性测量的应用程序。最初,我使用了一大堆“double”类型的变量,并以匈牙利符号或在我的脑海中跟踪值:

Public Class ResultData
Public Property position_in_mm_vs_time_in_ms as Double(,)
Public Property initial_position_in_mm as Double
Public Property final_position_in_mm as Double
Public Property duration_in_ms as Double
Public Property speed_in_mm_per_s as Double
End Class

但是随着我添加更多的项目和更多的属性,这很快就变得一团糟,到处都是转换因子,无法知道一个值是米还是毫安,没有硬编码就无法知道一个项目的正确缩写是什么和手动跟踪,以及添加选项以 SI 或英制单位输入和输出数据的想法令人恐惧。

我意识到这个问题是一个打字问题,我可以使用一个具有类型和值的类来改进这种安排:
Namespace Measure
Public Class Value(Of GenericUnits)
Public Property Value As Double
End Class
Public Class ValuePoint(Of XUnits, YUnits)
Public X As Value(Of XUnits)
Public Y As Value(Of YUnits)
Public Sub New(x As Value(Of XUnits), y As Value(Of YUnits))
Me.X = x
Me.Y = y
End Sub
End Class
Public Class Units
Public Interface GenericUnits
ReadOnly Property Abbreviation As String
' Additional properties, operators, and conversion functions
End Interface
' Additional unit types
End Class
End Namespace

所以我的声明变成了:
Public Class ResultData
Public Property PositionData as List(of ValuePoint(of Units.Seconds, Units.Millimeters))
Public Property InitialPosition as Value(of Units.Millimeters)
Public Property FinalPosition as Value(of Units.Millimeters)
Public Property Duration as Value(of Units.Milliseconds)
Public Property Speed as Value(of Units.MillimetersPerSecond)
End Class

这真的很好很干净。现在我想使用我的运营商定义的属性和转换,但我不能:
Dim result As New ResultData()
Dim msg As New System.Text.StringBuilder()
msg.AppendLine("Speed units are abbreviated as: ")
msg.AppendLine(result.Speed.GetType().ToString() & "?")
msg.AppendLine(result.Speed.GetType().GenericTypeArguments(0).ToString() & "?")
' Produces error "Abbreviation is not a member of System.Type"
' Casting produces conversion error
'msg.AppendLine(result.Speed.GetType().GenericTypeArguments(0).Abbreviation & "?")
' Produces:
' Speed units are abbreviated as:
' Measure.Value`1[Measure.Units+MillimetersPerSecond]
' Measure.Units+MillimetersPerSecond
MsgBox(msg.ToString())

如何访问我的类型声明的属性和方法?

我发现我的声明 Value(Of GenericUnits)实际上并没有引用名为 GenericUnits 的接口(interface), 而是生成一个泛型类型;我不妨称之为 Value(Of T) .我认为这可能与我的问题有关。

最佳答案

您需要约束您的泛型从您的接口(interface)派生:

Public Class Measurement(Of T as IGenericUnits) 'class name was Value before edit 
Public Property Value As Double
Public Property UOM As IGenericUnits 'UOM is a common abbreviation for unit-of-measure
End Class

有关泛型和约束的附加信息,请参见此处: http://msdn.microsoft.com/en-us/library/w256ka79.aspx

编辑:

用法如下:
msg.AppendLine(result.Speed.UOM.Abbreviation)

几个建议:
  • 重命名 GenericUnitsIGenericUnits遵循推荐的命名约定
  • 找到比 Value" for your measurement class. 更好的名字值(value) is used all over the place and you are bound to run into a naming conflict eventually. Perhaps测量 or MeasuredValue` 代替。
  • 关于.net - 测试该泛型类型实现一个接口(interface)并使用该接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19249765/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com