我有一个关于通用类和相关运算符重载定义的问题。
问题是:
如果假设 DataType 为任意数值类型(如float、double、Integer等),如何设置以下类的运算符?
目前我还不太理解这个概念。
Public Class Nombre(Of DataType) '' aka Number
'VALUE
Private _Value As DataType
Public Property Value As DataType
Get
Return Me._Value
End Get
Set(value As DataType)
Me._Value = value
End Set
End Property
Public Sub New(value As DataType)
Me._Value = value
End Sub
Public Sub Add(x As DataType)
_Value += x
End Sub
Public Sub Subs(x As DataType)
_Value -= x
End Sub
Public Sub Mult(x As DataType)
_Value = _Value * x
End Sub
Public Sub Power(x As DataType)
_Value = _Value ^ x
End Sub
Public Sub inc()
_Value += 1
End Sub
Public Sub dec()
_Value -= 1
End Sub
Public Sub Divide(x As DataType)
_Value = _Value / x
End Sub
End Class