操作符重载

时间:2014-04-28 14:49:57   收藏:0   阅读:477

  

bubuko.com,布布扣
    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;
        }

    }
bubuko.com,布布扣

 

操作符重载,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!