码迷,mamicode.com
首页 > 其他好文 > 详细

操作符重载

时间:2014-04-28 14:49:57      阅读:477      评论:0      收藏:0      [点我收藏+]

标签:com   http   class   blog   style   div   img   code   java   javascript   string   

  

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

操作符重载

标签:com   http   class   blog   style   div   img   code   java   javascript   string   

原文地址:http://www.cnblogs.com/haomo/p/3695414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!