委托使用心得

来源:互联网 发布:东盟共同体 知乎 编辑:程序博客网 时间:2024/06/05 18:46

非静态方法的委托使用实例 

//声明一个代理 返回类型为void 当然也可以为 int string bool...

//必须要注意的一点:声明委托的时候委托的参数类型和个数要与方法的参数类型个数一致

delegate void Eat(string fruit);

    class Program
    {
        static void Main(string[] args)
        {
            //声明并实例化三个人
            Man a = new Man("张三");
            Man b = new Man("李四");
            Man c = new Man("王五");
            //将Man类里面的非静态方法委托给Eat,也就是让Eat具有EatFruit方法的代理
           //将三个对象对应的方法的地址添加到同一个委托中用“+”表示
            //取消对某个方法的委托代理用“-”,看了一下代码就会明白了
            Eat eat = a.EatFruit;//另外一种写法: Eat e = new Eat(a.EatFruit);笔者认为前种跟省事
            eat += b.EatFruit;
            eat += c.EatFruit;
            //下面是使用委托实现对三个实例方法的实现
            eat("苹果");
            //如果张三不想吃水果了就取消对其方法的代理
            eat -= a.EatFruit;
            //再看委托运行
            Console.WriteLine("\r\n张三走了");
            eat("香蕉");
            
            Console.ReadLine();
        }
        //把委托封装到一个方法里面
        static void EatTogether(string fruit,params Eat[] actions)
        { 
            //判断委托数组是否有方法
            if (actions == null)
            {
                Console.WriteLine("人都走了,没人吃水果了!");
            }
            else
            {
                Eat eat = null;
                foreach (Eat e in actions)
                {
                    eat += e;
                }
                eat(fruit);
            }


        }
    }
    class Man
    {
        public string Name;
        //重写构造函数并给出这个类实例的名字<Name>
        public Man(string name)
        {
            this.Name = name;
        }
        //此实例吃水果
        public void EatFruit(string fruit)
        {
            Console.WriteLine(Name + "吃" + fruit);
        }

    }

是不是觉得这很好用啊,再看下面的

 delegate void Eat(string fruit);
    class Program
    {
        static void Main(string[] args)
        {
          
            Man a = new Man("张三");
            Man b = new Man("李四");
            Man c = new Man("王五");
            Eat eat = a.EatFruit;
            eat += b.EatFruit;
            eat += c.EatFruit;
           //对封装了委托的方法的调用,可变数组的使用
            EatTogether("苹果", a.EatFruit, b.EatFruit, c.EatFruit);  

          //人都走了没人吃了
            EatTogether(null, null);

            Console.ReadLine();
        }
        //把委托封装到一个方法里面
        static void EatTogether(string fruit,params Eat[] actions)
        { 
            //判断委托数组是否有方法
            if (actions == null)
            {
                Console.WriteLine("人都走了,没人吃水果了!");
            }
            else
            {
                Eat eat = null;
                foreach (Eat e in actions)
                {
                    eat += e;
                }
                eat(fruit);
            }


        }
    }
    class Man
    {
        public string Name;
        //重写构造函数并给出这个类实例的名字<Name>
        public Man(string name)
        {
            this.Name = name;
        }
        //此实例吃水果
        public void EatFruit(string fruit)
        {
            Console.WriteLine(Name + "吃" + fruit);
        }
    }

下面在介绍一种无参无返回值的委托用法

 class Program
    {


        static void Main(string[] args)
        {
           //对Action委托的使用
            Action d = One;
            d += Two;
            d += Three;
            Delegate[] delegates = d.GetInvocationList();//Deegate类定义的GetInvocationList()方法,它返回一个Delegate对想数组
            foreach (Action dl in delegates)
            {
                try
                {
                    dl();
                }
                catch (Exception)
                {


                }


            }
            Console.ReadLine();
        }
        static void One()
        {
            Console.WriteLine("One");
        }
        static void Two()
        {
            Console.WriteLine("Two");
        }
        static void Three()
        {
            Console.WriteLine("Three");
        }


    }

其实委托还有很多种用法如 Func<in T,out TResult>   Action<T> 以及匿名方法的委托使用

1 0
原创粉丝点击