首页 > 代码库 > 操作符重载

操作符重载

  

    class Program
    {
        int count;

        public Program() { }
        public Program(int count)
        {
            this.count = count;
        }

        static void Main(string[] args)
        {
            Program p1 = new Program(16);
            Program p2 = new Program(16);

            /** 操作符调用 */
            Console.WriteLine(p1 + p2);

            Console.WriteLine(p1 == p2);
            p2.count++;
            Console.WriteLine(p1 == p2);
        }
        /// <summary>
        /// 操作符重载
        /// </summary>
        /// <param name="num1">参数一</param>
        /// <param name="num2">参数二</param>
        /// <returns></returns>
        public static int operator +(Program num1, Program num2)
        {
            return num1.count + num2.count;
        }

        /// <summary>
        /// 操作符重载
        /// </summary>
        /// <param name="num1">参数一</param>
        /// <param name="num2">参数二</param>
        /// <returns></returns>
        public static bool operator ==(Program num1, Program num2)
        {
            return num1.count == num2.count;
        }

        /// <summary>
        /// 操作符重载
        /// </summary>
        /// <param name="num1">参数一</param>
        /// <param name="num2">参数二</param>
        /// <returns></returns>
        public static bool operator !=(Program num1, Program num2)
        {
            return num1.count != num2.count;
        }

    }