涕淌居

牛~冬~@~瘋~牛~涕~淌~の~涕~淌~居~~GnuDoyng's Web Log~~~Contact me~niu_dong@msn.com~ICQ:277276843~QQ:123930135~

原创 [译]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.

发表于 @ 2004年12月30日 22:31:00|评论(loading...)

新一篇: 狂晕~GRE报名确认信被MSN扔到Junk堆里去了 | 旧一篇: 关于Visual Basic 2005 Beta中窗体互访技术的思索

用户操作
[即时聊天] [发私信] [加为好友]
疯牛涕淌
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
疯牛涕淌的公告
123930135~有蝦米代誌請Q我哦! 本blog已搬家...
文章分类
收藏
百科全書
Britannica
Encyclopedia Mythica
Encyclopedia of Philosophy
Encyclopedia.com
MSN Encarta
WIKIPEDIA
大師之家
Code/Tea/Etc...
Mind View
Panopticon Central
Robert Green's VB Blog
Rockford Lhotka
The Mountain of Worthless Information
The VB Team
VB.NET Expert
侯捷網站
話語資源
American Dialects
Ethnologue
Glossary
IPA HTML Entities
Lexicon of Linguistics
The Linguistic Society of America
Übungen Für Deutsch
Word 2 Word
WordNet
Yamada Language Guides
yourDictionary
技術社區
.NET Framework Windows Forms
ASP.NET Heaven
ASP.NET Web
C# Corner
C# Friends
C# Help
Code Guru
Dev Guru
Developer Fusion
DotNet Zone
Got DotNet
MIT開放式課程網頁
Most Valuable Professional
MSDN
SuperSite for Windows
The Code Project
VB Forums
VB Info Zine
VB.NET Heaven
VBCity
中國 Dot Net Nuke
微軟中國社區
外國朋友
Bram Blogs
Дневник elishena
Добре дошли, приятели!
Заметки обалдевшей от счастья матери
友情鏈接
Amoeba原蟲
kgd jeyp
Let's Eat Noodles at Last
Nature Is Restless
YOYO Forever
三生石坊
去日留痕
咖啡愛我
夢想飛翔
天高任我飛
奔騰不息
宜軒品
寧靜*海豚灣
形而上的憂傷
我愛茉莉
我的生活
既生甲何生乙
明天的寂寞
沫言沫語
異域星空
裝配中的腦袋
閩中龍
阿貴
風一代
與我有關
Linguistics Paradise
UÕH UỎNG SANG
閩東語論壇
只是路過
安妮寶貝
微微微笑
存档
软件项目交易
Csdn Blog version 3.1a
Copyright © 疯牛涕淌