这是我要开发的代码:
Public Structure Statistic(Of t)
Dim maxStat As t
Dim curStat As t
Public Sub New(ByVal pValue As t)
maxStat = pValue
curStat = pValue
End Sub
Public Property Level() As t
Get
Return curStat
End Get
Set(ByVal value As t)
curStat = value
If curStat > maxStat Then curStat = maxStat
End Set
End Property
End Structure
它不会编译,因为我得到一个错误,'>' is not defined for types of T and T。无论如何我可以指定约束来保证 T 是数字类型吗?
这是经过用户的修改和建议后我目前得到的。它仍然无法正常工作。我是否必须将 T 的值更改为 IComparable?一定有一些非常简单的事情我搞砸了。
Public Structure Statistic(Of T As IComparable(Of T))
Dim maxStat As T
Dim curStat As T
Public Sub New(ByVal pValue As T)
maxStat = pValue
curStat = pValue
End Sub
Public Property Statistic() As T
Get
Return curStat
End Get
Set(ByVal value As T)
If value > maxStat Then
End If
End Set
End Property
End Structure
最佳答案
您可以将 T 限制为 IComparable .这样你就知道 curStat 和 maxStat 都有一个 CompareTo 方法,你可以用它来确定一个是否大于另一个。
关于.net - 通用类 (T) - 从一系列类型 VB.Net 中指定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3358416/