標題:
[分享]
運算子的重載
[打印本頁]
作者:
呆呆
時間:
2010-12-4 12:23
標題:
運算子的重載
VB.2005 以上有一項不錯用的功能,叫運算子的重載
什麼是運算子的重載
其實就是運算符號的重新定義
這在數學上很常見
一般我們在數學上 + - * / 這些東西是指數字的 加減乘除
但如果到了向量,或矩陣,或是虛數,這些符號就重新定義了
因為向量的加減乘除法和純量顯然有很大的不同,所以
A向量+B向量的程式碼不會寫成 C = A+B
但我們為了要使程式碼簡單易明白,所以我們也很想要這樣寫,那就必需重新定義運算子
[ <attributes> ] Public [ Overloads ] Shared [ Shadows ] _
[ Widening | Narrowing ] Operator symbol ( operands ) As type
...
End Operator
那這個要怎麼用,我們可以新增一個WindowsForm 專案
然後在該專案中再新增一個 Vector3D 類別如下
Public Class Vector3D
Private mXValue As Double
Public Property XValue() As Double
Get
Return mXValue
End Get
Set(ByVal value As Double)
mXValue = value
End Set
End Property
Private mYvalue As Double
Public Property YValue() As Double
Get
Return mYvalue
End Get
Set(ByVal value As Double)
mYvalue = value
End Set
End Property
Private mZvalue As Double
Public Property ZValue() As Double
Get
Return mZvalue
End Get
Set(ByVal value As Double)
mZvalue = value
End Set
End Property
Public ReadOnly Property NormOfVector() As Double
Get
Return Math.Sqrt(XValue * XValue + YValue * YValue + ZValue * ZValue)
End Get
End Property
Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double)
mXValue = x
mYvalue = y
mZvalue = z
End Sub
Public Sub New()
XValue = 0
YValue = 0
ZValue = 0
End Sub
Public Shared Operator +(ByVal A As Vector3D, ByVal B As Vector3D) As Vector3D
Return New Vector3D(A.XValue + B.XValue, A.YValue + B.YValue, A.ZValue + B.ZValue)
End Operator
Public Shared Operator -(ByVal A As Vector3D, ByVal B As Vector3D) As Vector3D
Return New Vector3D(A.XValue - B.XValue, A.YValue - B.YValue, A.ZValue - B.ZValue)
End Operator
'外積
Public Shared Operator *(ByVal A As Vector3D, ByVal B As Vector3D) As Vector3D
Dim tmpVector As New Vector3D
With tmpVector
.XValue = A.YValue * B.ZValue - A.ZValue * B.YValue
.YValue = -(A.XValue * B.ZValue - A.ZValue * B.XValue)
.ZValue = A.XValue * B.YValue - A.YValue * B.XValue
End With
Return tmpVector
End Operator
'內積
Public Shared Operator ^(ByVal A As Vector3D, ByVal B As Vector3D) As Double
Return A.XValue * B.XValue + A.YValue * +B.YValue + A.ZValue * B.ZValue
End Operator
Public Shared Function AngleBetweenTwoVector(ByVal A As Vector3D, ByVal B As Vector3D) As Double
Return Math.Acos((A ^ B) / (A.NormOfVector * B.NormOfVector)) * 180 / Math.PI
End Function
Public Overrides Function ToString() As String
If Me.YValue < 0 And Me.ZValue < 0 Then
Return Me.XValue & "i - " & -Me.YValue & "j - " & -Me.ZValue & "k"
End If
If Me.YValue < 0 And Me.ZValue >= 0 Then
Return Me.XValue & "i - " & -Me.YValue & "j + " & Me.ZValue & "k"
End If
If Me.YValue >= 0 And Me.ZValue < 0 Then
Return Me.XValue & "i + " & Me.YValue & "j - " & -Me.ZValue & "k"
End If
Return Me.XValue & "i + " & Me.YValue & "j + " & Me.ZValue & "k"
End Function
End Class
複製代碼
作者:
呆呆
時間:
2010-12-4 12:30
上面的程式碼我們重定義了乘法的內外積和加減法
還有一個二向量的夾角分享方法
因為向量沒有除法所以不用定義,而乘法分為內外積二種
所以我們可以在 WindowsForm 上寫下以下程式碼
Public Class Form1
Private A As New Vector3D(2, 3, 2)
Private B As New Vector3D(1, 2, -1)
Private C As New Vector3D
Private D As Double
Private H As Double
Private fnt As Font
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
fnt = New Font("新細明體", 30)
C = A * B '外積
D = A ^ B '內樍
H = Vector3D.AngleBetweenTwoVector(A, B) '夾角
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
g.DrawString("二向量外積為 " & C.ToString, fnt, New SolidBrush(Color.Black), 10, 100)
g.DrawString("二向量內積為 " & D.ToString, fnt, New SolidBrush(Color.Black), 10, 200)
g.DrawString("二向量夾角為 " & H.ToString & " 度", fnt, New SolidBrush(Color.Black), 10, 300)
End Sub
End Class
複製代碼
執行結果如下
作者:
呆呆
時間:
2010-12-4 12:34
看起來是不是比較符合我們一般算數學的符號
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)