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

        }

    }

}

你可能感兴趣的:(运算符)