Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
• Một phần tên là: lắp ráp có chứa chỉ có tên lắp ráp ngắn và một số phiên bản. Chia sẻ hội đồng phải được đặt tên là mạnh mẽ, và chỉ mạnh mẽ tên là hội đồng Ví dụ này hiển thị các chuỗi bằng cách sử dụng các loại MessageBox quy định tại Windows.Forms, kể từ khi giao diện điều khiển không phải là hiển thị các ký tự Unicode. | CHAPTER 8 OPERATOR OVERLOADING 149 Public Sub New ByVal real As Double ByVal imaginary As Double Me.Real real Me.Imaginary imaginary End Sub System.Object override Public Overrides Function ToString As String Return System.String.Format 0 1 Real Imaginary End Function Public ReadOnly Property Magnitude As Double Get Return Math.Sqrt Math.Pow Me.Real 2 Math.Pow Me.Imaginary 2 End Get End Property Public Shared Operator IsTrue ByVal c As Complex As Boolean Return c.Real 0 OrElse c.Imaginary 0 End Operator Public Shared Operator IsFalse ByVal c As Complex As Boolean Return c.Real 0 AndAlso c.Imaginary 0 End Operator Public Shared Widening Operator CType ByVal d As Double As Complex Return New Complex d 0 End Operator Public Shared Narrowing Operator CType ByVal c As Complex As Double Return c.Magnitude End Operator Other methods omitted for clarity. End Structure Public Class EntryPoint Shared Sub Main Dim cpx1 As Complex New Complex 1.0 3.0 If cpx1 Then Console.WriteLine cpx1 is True Else Console.WriteLine cpx1 is False End If Dim cpx2 As Complex New Complex 0.0 0.0 150 CHAPTER 8 OPERATOR OVERLOADING Console.WriteLine cpx2 is 0 IIf cpx2 True False End Sub End Class This code displays the following results cpx1 is True cpx2 is False You can see the two operators for applying the IsTrue and IsFalse tests to the Complex type. Notice that the declaration syntax looks almost the same as the conversion operators except it includes the return type of Boolean. Also you cannot mark these operators as Widening or Narrowing because they re not conversion operators. Once you define these two operators on the type you can use instances of Complex in Boolean test expressions as shown in the Main method. Note In setting Option Strict Off in the previous example you re forcing the narrowing conversion to Boolean to be accepted by the compiler. You should do this only if you re certain that a runtime error is not possible. Alternatively you can choose to implement a conversion to .