1、运算符重载的关键字:operator
2、是一个公共的、静态的方法
3、运算符重载方法写在一个类里,且其中一个参数必然是这个类
4、返回值卸载operator前
5、不能使用ref或out
6、语法:
public static 返回值 operator 符号(参数列表)
public class Point
{
public int x;
public int y;
public static Point operator +(Point P1, Point P2)
{
Point point= new Point();
point.x = P1.x+P2.x;
point.y = P1.y+P2.y;
return point;
}
}
internal class Program
{
static void Main(string[] args)
{
Point p1= new Point();
p1.x = 1;
p1.y = 2;
Point p2= new Point();
p2.x = 3;
p2.y = 4;
Point p3= new Point();
p3 = p1+ p2;
Console.WriteLine(p3.x+" , "+p3.y);
}
}
7、可重载和不可重载运算符
算术运算符都可以重载
逻辑运算符只有!可以重载
条件运算符必须成对实现