e媒网络

一切皆可能 e媒网络 http://www.eMay.net

博客园 首页 新随笔 联系 订阅 管理

参考代码1:

using System;

namespace DelegateDemo
{
    class Program
    {
        public delegate void Expresser();
        static void Main(string[] args)
        {
            //委托, delegate  
            Test t = new Test();
            Expresser exp1 = new Expresser(t.MyExpressApple);
            Expresser exp2 = new Expresser(t.MyExpressMail);
            Expresser exp3 = exp2;
            exp1();
            exp3();
            /////////////////////////////////////
            Expresser exp11 = new Expresser(Test2.MyExpressBook);
            Expresser exp22 = new Expresser(Test2.MyExpressPen);
            exp11();
            exp22();
        }
    }
    class Test
    {
        public  void MyExpressMail()
        {
            Console.WriteLine("寄送邮件");
        }
        public  void MyExpressApple()
        {
            Console.WriteLine("寄送苹果");
        }       
    }
    class Test2
    {
        public static void MyExpressBook()
        {
            Console.WriteLine("寄送书籍");
        }
        public static void MyExpressPen()
        {
            Console.WriteLine("寄送笔");
        }
    }

}

参考代码2:

using System;

namespace ArithDemo
{
    class Program
    {
        public delegate double ArithDelegater(int a,int b);
        
        static void Main(string[] args)
        {
            ArithDelegater[] a= new ArithDelegater[4];
            a[0]= new ArithDelegater(Arith.Add);
            a[1] = new ArithDelegater(Arith.Sub);
            a[2] = new ArithDelegater(Arith.Mul);
            a[3] = new ArithDelegater(Arith.Div);

            for(int i=0;i<4;i++)
            {
                Console.WriteLine(a[i](100,30));
            }          
      
        }
    }
    class Arith
    {
        public static  double  Add(int a,int b)
        {
            return a + b;
        }
        public static double Sub(int a, int b)
        {
            return a - b;
        }
        public static double Mul(int a, int b)
        {
            return a * b;
        }
        public static double Div(int a, int b)
        {
            return (double)a / b;
        }
    }
}

 参考代码3:

using System;

namespace ArithDemo
{
    class Program
    {
        public delegate void ArithDelegater(int a, int b);

        static void Main(string[] args)
        {
           ArithDelegater a = new ArithDelegater(Arith.Add);        
           a+= new ArithDelegater(Arith.Sub);
           a+= new ArithDelegater(Arith.Mul);
           a+= new ArithDelegater(Arith.Div); 
           a(100, 30);
        Console.WriteLine("--------------");
           a.Invoke(1000, 300); } }
class Arith { public static void Add(int a, int b) { Console.WriteLine(a + b); } public static void Sub(int a, int b) { Console.WriteLine(a - b); } public static void Mul(int a, int b) { Console.WriteLine(a * b); } public static void Div(int a, int b) { Console.WriteLine((double)a / b); } } }

 

posted on 2022-11-24 16:23  e媒网络技术团队  阅读(48)  评论(0编辑  收藏  举报