[译]Visual Basic 2005在语言上的增强(七)运算符重载和转换运算符

在Visual Basic 2005的诸多增强的功能中,运算符的重载是最强大的之一。运算符重载允许在任何数据类型上定义运算符,甚至是对你自己创建的基类型进行运算。

最经典的运算符重载的例子就是定义复数的相加。一个简化了的拥有“+”号重载运算符的Complex类,可能是下面这种形式:
Public Class Complex

    Public Real As Double
    Public Imag As Double

    Public Sub New(ByVal realPart As Double, ByVal imagPart As Double)
        Real = realPart
        Imag = imagPart
    End Sub

    Shared Operator +(ByVal lhs As Complex, ByVal rhs As Complex) As Complex
        Return New Complex(lhs.Real + rhs.Real, lhs.Imag + rhs.Imag)
    End Operator

End Class

现在,我们可以用非常直觉的方式,用“+”号运算符来使两个复数相加:
Dim lhs As Complex = New Complex(2.0, 2.5)
Dim rhs As Complex = New Complex(3.0, 3.5)
Dim res As Complex = lhs + rhs 'res.real = 5.0, res.imag = 6.0

你也可以为你的自定义类型重载转换运算符。如果你试图把上面的res的值按照CType(res, String)转换,你会得到一个编译错误告诉你Complex类无法转换成String。如果考虑使用res.ToString的值的话,你会发现复数值的ToString方法返回的只是它的默认类型名。你可以通过重载CType转换运算符和ToString方法来解决这些问题,就像这样:
Public Shared Narrowing Operator CType(ByVal Value As Complex) As String
    '这里的转换没有特殊的要求,因此也可以把Narrowing关键字改成Widening关键字,涕淌注
    Return Value.Real.ToString & "i" & Value.Imag.ToString
End Operator

'涕淌注:作者原文的这段代码有误,无法实现覆盖基类Object类型的ToString方法!
Public Overrides Function ToString(ByVal Value As Complex) As String
    Return Value.Real.ToString & "i" & Value.Imag.ToString
End Function

'涕淌注:正确的代码如下!
Public Overrides Function ToString() As String
    Return Real.ToString & "i" & Imag.ToString
End Function

现在CType(res, String)和res.ToString的返回值就和我们想要的一样了:“5.0i6.0”(实际得到的是“5i6”,Visual Basic把最后的点零省略了,涕淌注)。转换运算符声明时必须要么被Narrowing修饰,要么被Widening修饰,以此来说明转换的性质。

@以下是原文供大家参考@

Operator Overloading and Conversion Operators

One of the most powerful additions to Visual Basic 2005 is operator overloading. Operator overloading lets you define operators that act on any type you want, even to the point of creating your own base types.

The classic operator-overloading example is adding complex numbers. A simplified Complex class with the + operator overloaded might look like this:
Public Class Complex

    Public Real As Double
    Public Imag As Double

    Public Sub New(ByVal realPart As Double, ByVal imagPart As Double)
        Real = realPart
        Imag = imagPart
    End Sub

    Shared Operator +(ByVal lhs As Complex, ByVal rhs As Complex) As Complex
        Return New Complex(lhs.Real + rhs.Real, lhs.Imag + rhs.Imag)
    End Operator

End Class

Use the + operator to add complex numbers in a very intuitive way:
Dim lhs As Complex = New Complex(2.0, 2.5)
Dim rhs As Complex = New Complex(3.0, 3.5)
Dim res As Complex = lhs + rhs 'res.real = 5.0, res.imag = 6.0

You can also overload conversion operators for your custom types. If you try to convert the value of res as CType(res, String), you'll get a compiler error Complex cannot be converted to String." If you try looking at the value of res.ToString, you get a value of Complex back as ToString shows the type name by default. You fix these issues by overloading the CType conversion operator and the ToString method, like so:
Public Shared Narrowing Operator CType(ByVal Value As Complex) As String
    Return Value.Real.ToString & "i" & Value.Imag.ToString
End Operator

Public Overrides Function ToString(ByVal Value As Complex) As String
    Return Value.Real.ToString & "i" & Value.Imag.ToString
End Function

Now the returned values of CType(res, String) and res.ToString are what you would expect: "5.0i6.0." Conversion operators must be declared with either Narrowing or Widening, indicating how the conversion is done.

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值