.NET重载运算符

代码如下:

  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);

 运行结果:

posted @ 2016-10-29 08:36  $("#阿飞")  阅读(427)  评论(0编辑  收藏  举报