• 运算符重载的一个例子


    class MainClass
    {
        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 = rhs.x;
                y = rhs.y;
                z = rhs.z;
            }

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

            public static Vector operator +(Vector lhs, Vector rhs)
            {
                Vector result = new Vector(lhs);
                result.x += rhs.x;
                result.y += rhs.y;
                result.z += rhs.z;
                return result;
            }
        }
        static void Main()
        {
            Vector vect1, vect2, vect3;
            vect1 = new Vector(3.0, 3.0, 1.0);
            vect2 = new Vector(2.0, -4.0, -4.0);
            vect3 = vect1 + vect2;
            Console.WriteLine("vect1=" + vect1.ToString());
            Console.WriteLine("vect2=" + vect2.ToString());
            Console.WriteLine("vect3=" + vect3.ToString());


        }

    }

    输出结果:

    vect1=(3,3,1)
    vect2=(2,-4,-4)
    vect3=(5,-1,-3)

  • 相关阅读:
    split a string into an array through comma
    正则表达式替换日期
    在Ajax1.0中调用页面CS文件中的方法
    半透明的div对话框
    foreach 的自动转化类型
    ViewStateAutoManager
    using ISerializable to control serialization and deserialization
    div with separated html template
    2018.9.9作业
    CSS单位
  • 原文地址:https://www.cnblogs.com/tianguook/p/2002417.html
Copyright © 2020-2023  润新知