.NET重載運算子

來源:互聯網
上載者:User

標籤:csharp   座標   summary   ram   對象   cat   static   image   span   

代碼如下:

  1  /// <summary>  2     /// 座標(結構類型)  3     /// </summary>  4     public struct Coordinate  5     {  6         public int x;  7         public int y;  8   9         public Coordinate(int x, int y)  10         { 11             this.x = x; 12             this.y = y; 13         } 14  15         /// <summary> 16         /// 對象存在於集合中 17         /// </summary> 18         /// <param name="list"></param> 19         /// <returns></returns> 20         public bool IsOneOf(List<Coordinate> list) 21         { 22             if (list.IndexOf(this) >= 0) 23             { 24                 return true; 25             } 26             return false; 27         } 28  29         /// <summary> 30         /// 比較兩個座標相等 31         /// </summary> 32         /// <param name="c"></param> 33         /// <returns></returns> 34         public override bool Equals(object c) 35         { 36             if (c.GetType() != this.GetType()) 37                 return false; 38  39             Coordinate coordinate = (Coordinate)c; 40  41             if (this.x != coordinate.x) 42                 return false; 43  44             if (this.y != coordinate.y) 45                 return false; 46  47             return true; 48         } 49  50         /// <summary> 51         /// 比較兩個座標相等 52         /// </summary> 53         /// <param name="lhs"></param> 54         /// <param name="rhs"></param> 55         /// <returns></returns> 56         public static bool operator ==(Coordinate lhs, Coordinate rhs) 57         { 58             return lhs.Equals(rhs); 59         } 60  61         /// <summary> 62         /// 比較兩個座標不相等 63         /// </summary> 64         /// <param name="lhs"></param> 65         /// <param name="rhs"></param> 66         /// <returns></returns> 67         public static bool operator !=(Coordinate lhs, Coordinate rhs) 68         { 69             return !lhs.Equals(rhs); 70         } 71  72         /// <summary> 73         /// 兩個座標相加,獲得新座標 74         /// </summary> 75         /// <param name="lhs"></param> 76         /// <param name="rhs"></param> 77         /// <returns></returns> 78         public static Coordinate operator +(Coordinate lhs, Coordinate rhs) 79         { 80             return new Coordinate() 81             { 82                 x = lhs.x + rhs.x, 83                 y = lhs.y + rhs.y 84             }; 85         } 86  87         /// <summary> 88         /// 兩個座標象減,獲得新座標 89         /// </summary> 90         /// <param name="lhs"></param> 91         /// <param name="rhs"></param> 92         /// <returns></returns> 93         public static Coordinate operator -(Coordinate lhs, Coordinate rhs) 94         { 95             return new Coordinate() 96             { 97                 x = lhs.x - rhs.x, 98                 y = lhs.y - rhs.y 99             };100         }101 102         public override int GetHashCode()103         {104             return base.GetHashCode();105         }106     }
View Code

使用如下

            Coordinate coor1 = new Coordinate(10, 20);            Coordinate coor2 = new Coordinate(20, 30);            Console.WriteLine("coor1 == coor2:{0}", coor1 == coor2);            var coor3 = coor2 - coor1;            Console.WriteLine("coor2 - coor1獲得新座標【X:{0}】【Y:{1}】", coor3.x, coor3.y);            coor3 = coor2 + coor1;            Console.WriteLine("coor2 + coor1獲得新座標【X:{0}】【Y:{1}】", coor3.x, coor3.y);

 運行結果:

.NET重載運算子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.