tailieunhanh - Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition phần 4
Nếu bạn đang đến với C # từ một nền tảng C + +, bạn có thể than khóc sự mất mát quá tải người điều khiển giao viết tắt (+ =, -=, và vv). Đừng sợ. Trong điều kiện của C #, viết tắt các nhà khai thác chuyển nhượng tự động mô phỏng nếu một kiểu quá tải các nhà điều hành liên quan đến nhị phân. | Simpo PDF Merge and Split Unregistered Version - http 392 CHAPTER 12 INDEXERS OPERATORS AND POINTERS And What of the and - Operators If you are coming to C from a C background you may lament the loss of overloading the shorthand assignment operators - and so forth . Fear not. In terms of C the shorthand assignment operators are automatically simulated if a type overloads the related binary operator. Thus given that the Point structure has already overloaded the and - operators you are able to write the following Overloading binary operators results in a freebie shorthand operator. static void Main string args . Freebie Point ptThree new Point 90 5 ptThree 0 ptThree ptThree ptTwo 0 ptThree ptTwo Freebie - Point ptFour new Point 0 500 01 ptFour 0 ptFour ptFour - ptThree 0 ptFour - ptThree Overloading Unary Operators C also allows you to overload various unary operators such as and --. When you overload a unary operator you will also define a static method via the operator keyword however in this case you will simply pass in a single parameter that is the same type as the defining class structure. For example if you were to update the Point with the following overloaded operators public struct Point . Add 1 to the incoming Point. public static Point operator Point p1 return new Point 1 1 Subtract 1 from the incoming Point. public static Point operator -- Point p1 return new Point you could increment and decrement Point s x and y values as follows static void Main string args . Applying the and -- unary operators to a Point. Point ptFive new Point 1 1 ptFive 0 ptFive 2 2 --ptFive 0 --ptFive 1 1 Apply same operators as postincrement decrement. Point ptSix new Point 20 20 Simpo PDF Merge and Split Unregistered Version - http CHAPTER 12 INDEXERS OPERATORS AND POINTERS 393 .
đang nạp các trang xem trước