Board logo

標題: [分享] 運算子的重載 [打印本頁]

作者: 呆呆    時間: 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 類別如下
  1. Public Class Vector3D

  2.     Private mXValue As Double
  3.     Public Property XValue() As Double
  4.         Get
  5.             Return mXValue
  6.         End Get
  7.         Set(ByVal value As Double)
  8.             mXValue = value
  9.         End Set
  10.     End Property

  11.     Private mYvalue As Double
  12.     Public Property YValue() As Double
  13.         Get
  14.             Return mYvalue
  15.         End Get
  16.         Set(ByVal value As Double)
  17.             mYvalue = value
  18.         End Set
  19.     End Property

  20.     Private mZvalue As Double
  21.     Public Property ZValue() As Double
  22.         Get
  23.             Return mZvalue
  24.         End Get
  25.         Set(ByVal value As Double)
  26.             mZvalue = value
  27.         End Set
  28.     End Property

  29.     Public ReadOnly Property NormOfVector() As Double
  30.         Get
  31.             Return Math.Sqrt(XValue * XValue + YValue * YValue + ZValue * ZValue)
  32.         End Get
  33.     End Property

  34.     Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double)

  35.         mXValue = x
  36.         mYvalue = y
  37.         mZvalue = z

  38.     End Sub

  39.     Public Sub New()
  40.         XValue = 0
  41.         YValue = 0
  42.         ZValue = 0
  43.     End Sub

  44.     Public Shared Operator +(ByVal A As Vector3D, ByVal B As Vector3D) As Vector3D

  45.         Return New Vector3D(A.XValue + B.XValue, A.YValue + B.YValue, A.ZValue + B.ZValue)

  46.     End Operator


  47.     Public Shared Operator -(ByVal A As Vector3D, ByVal B As Vector3D) As Vector3D

  48.         Return New Vector3D(A.XValue - B.XValue, A.YValue - B.YValue, A.ZValue - B.ZValue)

  49.     End Operator

  50.     '外積
  51.     Public Shared Operator *(ByVal A As Vector3D, ByVal B As Vector3D) As Vector3D
  52.         Dim tmpVector As New Vector3D

  53.         With tmpVector
  54.             .XValue = A.YValue * B.ZValue - A.ZValue * B.YValue
  55.             .YValue = -(A.XValue * B.ZValue - A.ZValue * B.XValue)
  56.             .ZValue = A.XValue * B.YValue - A.YValue * B.XValue
  57.         End With
  58.         Return tmpVector
  59.     End Operator


  60.     '內積
  61.     Public Shared Operator ^(ByVal A As Vector3D, ByVal B As Vector3D) As Double
  62.         Return A.XValue * B.XValue + A.YValue * +B.YValue + A.ZValue * B.ZValue
  63.     End Operator


  64.     Public Shared Function AngleBetweenTwoVector(ByVal A As Vector3D, ByVal B As Vector3D) As Double

  65.         Return Math.Acos((A ^ B) / (A.NormOfVector * B.NormOfVector)) * 180 / Math.PI

  66.     End Function


  67.     Public Overrides Function ToString() As String

  68.         If Me.YValue < 0 And Me.ZValue < 0 Then
  69.             Return Me.XValue & "i - " & -Me.YValue & "j - " & -Me.ZValue & "k"
  70.         End If

  71.         If Me.YValue < 0 And Me.ZValue >= 0 Then
  72.             Return Me.XValue & "i - " & -Me.YValue & "j + " & Me.ZValue & "k"
  73.         End If

  74.         If Me.YValue >= 0 And Me.ZValue < 0 Then
  75.             Return Me.XValue & "i + " & Me.YValue & "j - " & -Me.ZValue & "k"
  76.         End If

  77.         Return Me.XValue & "i + " & Me.YValue & "j + " & Me.ZValue & "k"

  78.     End Function


  79. End Class
複製代碼

作者: 呆呆    時間: 2010-12-4 12:30

上面的程式碼我們重定義了乘法的內外積和加減法
還有一個二向量的夾角分享方法
因為向量沒有除法所以不用定義,而乘法分為內外積二種
所以我們可以在 WindowsForm 上寫下以下程式碼
  1. Public Class Form1

  2.     Private A As New Vector3D(2, 3, 2)
  3.     Private B As New Vector3D(1, 2, -1)
  4.     Private C As New Vector3D
  5.     Private D As Double
  6.     Private H As Double

  7.     Private fnt As Font

  8.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  9.         fnt = New Font("新細明體", 30)

  10.         C = A * B '外積
  11.         D = A ^ B '內樍
  12.         H = Vector3D.AngleBetweenTwoVector(A, B) '夾角

  13.     End Sub


  14.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

  15.         Dim g As Graphics = e.Graphics

  16.         g.DrawString("二向量外積為   " & C.ToString, fnt, New SolidBrush(Color.Black), 10, 100)
  17.         g.DrawString("二向量內積為   " & D.ToString, fnt, New SolidBrush(Color.Black), 10, 200)
  18.         g.DrawString("二向量夾角為   " & H.ToString & " 度", fnt, New SolidBrush(Color.Black), 10, 300)

  19.     End Sub

  20. End Class
複製代碼
執行結果如下
作者: 呆呆    時間: 2010-12-4 12:34

看起來是不是比較符合我們一般算數學的符號




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)