c#中的運算子多載

來源:互聯網
上載者:User
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. namespace CountStrDemo
    5. {
    6.     class 比較子重載
    7.     {
    8.         public static void Main(string[] args)
    9.         { 
    10.             // 比較預算符有6個,3對 分別是:
    11.             // ==  !=
    12.             // >  <
    13.             // >= <=
    14.             //成對重載,例如,重載了 == 則必須重載 != 反回類型必須是bool
    15.         }
    16.         class MyVectorForEq
    17.         {
    18.             public int x;
    19.             public int y;
    20.             public MyVectorForEq( int x , int y )
    21.             {
    22.                 this.x = x;
    23.                 this.y = y;
    24.             }
    25.             //重載==運算子 同時必須成對重載 !=
    26.             public static bool operator == ( MyVectorForEq lobj , MyVectorForEq robj )
    27.             {
    28.                 //這裡只是做了成員值的比較,ms推薦重寫Equals方法與配合hash來進行對象內容的比較
    29.                 if (lobj.x == robj.x && lobj.y == robj.y)
    30.                 {
    31.                     return true;
    32.                 }
    33.                 else
    34.                 {
    35.                     return false;
    36.                 }
    37.             }
    38.             public static bool operator !=( MyVectorForEq lobj , MyVectorForEq robj)
    39.             {
    40.                 if (lobj.x == robj.x && lobj.y == robj.y)
    41.                 {
    42.                     return false;
    43.                 }
    44.                 else
    45.                 {
    46.                     return true;
    47.                 }
    48.             }
    49.         }
    50.     }
    51. }

    using System;

  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace CountStrDemo
  4. {
  5.     class 運算浮重載
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             Vector vec1 = new Vector( 5 , 6 , 7 );
  10.             Vector vec2 = new Vector( 2 , 2 , 2 );
  11.             Vector revec = vec1 + vec2;
  12.             Console.WriteLine( "x={0} y={1} z={2}" , revec.x , revec.y , revec.z );
  13.             Console.Read();
  14.         }
  15.     }
  16.     class Vector
  17.     {
  18.         public int x;
  19.         public int y;
  20.         public int z;
  21.         public Vector( int x , int y  , int z )
  22.         {
  23.             this.x = x;
  24.             this.y = y;
  25.             this.z = z;
  26.         }
  27.         public Vector(Vector vec)
  28.         {
  29.             this.x = vec.x;
  30.             this.y = vec.y;
  31.             this.z = vec.z;
  32.         }
  33.         //定義重載 , 和方法類似,不過必須是public static ...可以記做沒有方法名,呵呵operator + 指重載+號運算
  34.         public static Vector operator + ( Vector lvec , Vector rvec )
  35.         {
  36.             Vector revec = new Vector( lvec );
  37.             revec.x += rvec.x;
  38.             revec.y += rvec.y;
  39.             revec.z += rvec.z;
  40.             return revec;
  41.         }
  42.     }
  43. }

 

-------------------------------------------------------------------------------------------------

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.