C# 运算符重载

重载运算符的类声明:

struct Vector
    {
        public double x, y, z;

        public Vector(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public Vector(Vector rhs)
        {
            x = this.x;
            y = this.y;
            z = this.z;
        }

        public override string ToString()
        {
            return "(" + x + "," + y + "," + z + ")";
        }

        public static Vector operator +(Vector lhs, Vector rhs)
        {
            Vector result = new Vector(rhs);
            result.x += rhs.x;
            result.y += rhs.y;
            result.z += rhs.z;

            return result;
        }

        public static Vector operator * (double lhs, Vector rhs)
        {
            return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
        }
    }
}
posted on 2009-07-30 11:23  ~数字人生~  阅读(187)  评论(0编辑  收藏  举报