您是第欢迎光临我的主页位访客
浩凡儿
天行健,君子以自强不息;地势坤,君子以厚德载物!



 概念:在C#中有一组用来完成内建类型基本操作的操作符。如:我们可以用+用于两个整数相加;如下 :

  int a=100;

 int b=240;

int sum=a+b; // 现在 c 就是340了;

这似乎很正常,但+可被用于大多数内建的c#类型。如下:

string  str1="Hello";

string str2="World";

string strAll=str1+str2;

 

这里的内部就是实现了操作符的重载。

 

下面我们举个例子来说明操作符重载:

 

View Code
  public class Point : IComparable
{
public int X { get; set; }
public int Y { get; set; }
public Point(int xPos, int yPos)
{
X = xPos;
Y = yPos;
}
public override string ToString()
{
return string.Format("[{0},{1}]", this.X, this.Y);
}

}

      

View Code
 //重载+操作符,它要求我们用operator关键字并且只能与static一起使用。
//而因为我们重载了+操作符所以+=也就一样有了重载了,不信看下面代码。
public static Point operator +(Point p1, Point p2)
{
return new Point(p1.X + p2.X, p1.Y + p2.Y);
}
public static Point operator -(Point p1, Point p2)
{
return new Point(p1.X - p2.X, p1.Y - p2.Y);
}


      

View Code
            //将传入的Point的X Y 值加1
public static Point operator ++(Point p)
{
return new Point(p.X + 1, p.Y + 1);
}

//将传入的Point 的X Y 的值减1
public static Point operator --(Point p)
{
return new Point(p.X - 1, p.Y - 1);
}

        

View Code
//现在让我们来重载==与!=操作符

/// <summary>
/// 重载==操作符
/// </summary>
/// <param name="p1">Point对象实例</param>
/// <param name="p2">Point对象实例</</param>
/// <returns>返回类型:bool; true/p1==p2 ; false/p1!=p2</returns>
public static bool operator ==(Point p1, Point p2)
{
return p1.Equals(p2);
}

/// <summary>
/// 重载!=操作符
/// </summary>
/// <param name="p1">Point对象实例</param>
/// <param name="p2">Point对象实例</</param>
/// <returns>返回类型:bool; true/p1!=p2 ; false/p1==p2</returns>
public static bool operator !=(Point p1, Point p2)
{
return !p1.Equals(p2);
}



     重载比较(<> <= >=)操作符 它和 ==or!= 是一样的必须成对重载。

       

View Code
 public int CompareTo(object obj)
{
if (obj is Point)
{
Point p = (Point)obj;
if (this.X > p.X && this.Y > p.Y)
{
return 1;
}
if (this.X < p.X && this.Y < p.Y)
{
return -1;
}
else
return 0;
}
else
throw new ArgumentException();
}

/// <summary>
/// 重载 < 操作符
/// </summary>
/// <param name="p1">Point对象实例</param>
/// <param name="p2">Point对象实例</param>
/// <returns>返回类型:bool;true:p1小于p2;false:p1大于p2</returns>
public static bool operator <(Point p1, Point p2)
{
return (p1.CompareTo(p2)) < 0;
}


     

View Code
   /// <summary>
/// 重载 < 操作符
/// </summary>
/// <param name="p1">Point对象实例</param>
/// <param name="p2">Point对象实例</param>
/// <returns>返回类型:bool;true:p1大于p2;false :p1小于p2</returns>
public static bool operator>(Point p1, Point p2)
{
return (p1.CompareTo(p2)) > 0;
}


       

View Code
 /// <summary>
/// 重载 < 操作符
/// </summary>
/// <param name="p1">Point对象实例</param>
/// <param name="p2">Point对象实例</param>
/// <returns>返回类型:bool;true:p1小于等于2;false:p1大于等于p2</returns>
public static bool operator<=(Point p1, Point p2)
{
return (p1.CompareTo(p2)) <= 0;
}


       

View Code
 /// <summary>
/// 重载 < 操作符
/// </summary>
/// <param name="p1">Point对象实例</param>
/// <param name="p2">Point对象实例</param>
/// <returns>返回类型:bool;true:p1大于等于p2;false :p1小于等于p2</returns>
public static bool operator >=(Point p1, Point p2)
{
return (p1.CompareTo(p2)) >= 0;
}

 

 

在Main方法中写下测试下:

 

View Code
 static void Main(string[] args)
{
Console.WriteLine("******** Fun with Overloaded Operators ********");

//创建两个节点
Point PtOne = new Point(100,100);
Point PtTwe = new Point(166,188);
Point PtThree = new Point(168,286);
Console.WriteLine("PtOne is : {0} .",PtOne);
Console.WriteLine("PtTwe is : {0} .",PtTwe);

//将两个点相加得到更大的点。
Console.WriteLine("PtOne + PtTwe is : {0} .",PtOne+PtTwe);
Console.WriteLine("PtThree += PtTwe is : {0} . ",PtThree+=PtTwe);
Console.WriteLine("Now PtOne is : {0} .", PtOne);
Console.WriteLine("++PtOne is : {0} .",++PtOne);
Console.WriteLine("Now PtOne is : {0} .", PtOne);
Console.WriteLine("PtOne++ is : {0} .", PtOne++);
Console.WriteLine("Now PtOne is : {0} .",PtOne);

//将两个点相减得到更小的点。
Console.WriteLine("PtOne - PtTwe is : {0} .",PtOne-PtTwe);
Console.WriteLine("PtThree -= PtTwe is : {0} . ",PtThree -= PtTwe);
Console.WriteLine("Now PtOne is : {0} .", PtOne);
Console.WriteLine("--PtOne is : {0} .", --PtOne);
Console.WriteLine("Now PtOne is : {0} .", PtOne);
Console.WriteLine("PtOne-- is : {0} .", PtOne--);
Console.WriteLine("Now PtOne is : {0} .", PtOne);


//相等操作符
Console.WriteLine("PtOne == PtTwe :{0}",PtOne==PtTwe);
Console.WriteLine("PtOne != PtTwe :{0}", PtOne != PtTwe);


//比较操作符
Console.WriteLine("PtOne < PtTwe :{0}",PtOne<PtTwe);
Console.WriteLine("PtOne >= PtTwe :{0}", PtOne > PtTwe);
Console.WriteLine("PtOne <= PtTwe :{0}", PtOne <= PtTwe);
Console.WriteLine("PtOne >= PtTwe :{0}", PtOne >= PtTwe);
Console.ReadLine();
}

 

 

posted on 2012-03-19 10:30  浩凡儿  阅读(3901)  评论(2编辑  收藏  举报