首页 > 其他 > 详细

操作符重载

时间:2014-04-28 14:48:58      阅读:494      评论:0      收藏:0      [点我收藏+]

  

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

操作符重载

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!