Chapter 8. 面向对象(类库、委托)

class FangFa
    {
        /// <summary>
        /// 加法运算
        /// </summary>
        /// <param name="a">第一个数</param>
        /// <param name="b">第二个数</param>
        /// <returns></returns>
        public int JiaFa(int a, int b)
        {
            return a + b;
        }

        /// <summary>
        /// 减法运算
        /// </summary>
        /// <param name="a">第一个数</param>
        /// <param name="b">第二个数</param>
        /// <returns></returns>
        public int JianFa(int a, int b)
        {
            return a - b;
        }
    }
    //声明委托类型
    public delegate int FirstDelegate(int a, int b); 

    class Program
    {
        static void Main(string[] args)
        {   
            //委托变量的定义及赋值
            FirstDelegate f1 = new FangFa().JiaFa;
            //使用委托
            int c1 = f1(5,4);
            Console.WriteLine("5+4="+c1);

            FirstDelegate f2 = new FangFa().JianFa;
            int c2 = f2(5, 4);
            Console.WriteLine("5-4="+c2);

            Console.ReadKey();
        }
    }

posted @ 2016-06-20 14:22  庚xiao午  阅读(84)  评论(0编辑  收藏  举报