c#重载运算符

来源:互联网 发布:大数据产品经理 编辑:程序博客网 时间:2024/05/16 00:26

public struct Point
{
    public    float x;
    public    float y;

    public Point (float x, float y)
    {
        this.x = x;
        this.y = y;
    }

    public Point Add (Point other)
    {
        return new Point (this.x + other.x, this.y + other.y);
    }

    public static Point Add (Point a, Point b)
    {
        return new Point (a.x + b.x, a.y + b.y);
    }
        
    public static Point operator + (Point a,Point b){
        return new Point (a.x + b.x, a.y + b.y);
    }

    public static Point operator - (Point a, Point b){
        return new Point (a.x - b.x, a.y - b.y);
    }

    public static bool operator == (Point a, Point b){
        if (a.x == b.x && a.y == b.y) {
            return true;
        }
        return false;
    }

    public static bool operator != (Point a, Point b){
        if (a.x == b.x && a.y == b.y) {
            return false;
        }
        return true;
    }
}
0 0