-
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- Namespace countstrdemo
- {
- Class comparison operator overload
- {
- Public static void main (string [] ARGs)
- {
- // There are 6 Comparison operators and 3 pairs:
- // =! =
- //> <
- // >=< =
- // Duplicate load. For example, if the value = is reloaded, it must be reloaded! = The reverse return type must be bool.
- }
- Class myvectorforeq
- {
- Public int X;
- Public int y;
- Public myvectorforeq (int x, int y)
- {
- This. x = X;
- This. Y = y;
- }
- // Overload = The operator must be reloaded in pairs at the same time! =
- Public static bool operator = (myvectorforeq lobj, myvectorforeq robj)
- {
- // Here we only compare the Member values. In ms, we recommend that you rewrite the equals method and use hash to compare the object content.
- If (lobj. x = robj. X & lobj. Y = robj. Y)
- {
- Return true;
- }
- Else
- {
- Return false;
- }
- }
- Public static bool Operator! = (Myvectorforeq lobj, myvectorforeq robj)
- {
- If (lobj. x = robj. X & lobj. Y = robj. Y)
- {
- Return false;
- }
- Else
- {
- Return true;
- }
- }
- }
- }
- }
Using system;
- Using system. Collections. Generic;
- Using system. text;
- Namespace countstrdemo
- {
- Class operation floating overload
- {
- Public static void main (string [] ARGs)
- {
- Vector vec1 = new vector (5, 6, 7 );
- Vector vec2 = new vector (2, 2, 2 );
- Vector revec = vec1 + vec2;
- Console. writeline ("x = {0} y = {1} z = {2}", revec. X, revec. Y, revec. z );
- Console. Read ();
- }
- }
- Class Vector
- {
- Public int X;
- Public int y;
- Public int Z;
- Public vector (int x, int y, int Z)
- {
- This. x = X;
- This. Y = y;
- This. z = z;
- }
- Public vector (vector VEC)
- {
- This. x = Vec. X;
- This. Y = Vec. Y;
- This. z = Vec. Z;
- }
- // Define the overload, which is similar to the method, but must be public static... you can remember that there is no method name, hehe operator + indicates the heavy load + number operation
- Public static vector operator + (vector lvec, vector rvec)
- {
- Vector revec = new vector (lvec );
- Revec. x + = rvec. X;
- Revec. Y + = rvec. Y;
- Revec. Z + = rvec. Z;
- Return revec;
- }
- }
- }
Bytes -------------------------------------------------------------------------------------------------