C# 委托代理静态的方法

来源:互联网 发布:淘宝号怎么注销手机号 编辑:程序博客网 时间:2024/06/03 15:59
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication7{    delegate void EatDelegate(string food); //创建一个代理    class Program    {        static void zsEat(string food) // 创建一个方法,方法和代理的返回值和参数必须一致:void、string       {           Console.WriteLine("张三吃" + food);        }        static void lsEat(string food)        {            Console.WriteLine("李四吃" + food);        }        static void wwEat(string food)        {            Console.WriteLine("王五吃" + food);        }        static void Main(string[] args)        {            EatDelegate zs = new EatDelegate(zsEat);//zs通过EatDelegate代理的函数为zsEat();            EatDelegate ls = new EatDelegate(lsEat);            EatDelegate ww = new EatDelegate(wwEat);            EatDelegate eatChain;//创建一个委托链            Console.WriteLine("张三、李四、王五开座谈会");            eatChain = zs + ls + ww;//将所有委托添加到委托链上            eatChain("西瓜");            Console.WriteLine(" ");            Console.WriteLine("李四出去了");            eatChain -= ls;//将一个委托从委托链上删除            eatChain("香蕉");            Console.WriteLine(" ");            Console.WriteLine("李四回来了");            eatChain += ls; //将一个委托添加到委托链上            eatChain("橘子");            Console.ReadLine();        }    }}/*使用委托的具体步骤*///1、声明一个委托,注意返回值和参数要与待委托的方法一致;//2、定义所有待委托的方法;//3、创建委托对象,并将待委托的方法包含在委托对象中;//4、通过委托对象调用各个方法。/*委托的声明*///格式: delegate 返回值类型 委托名(参数列表);/*委托对象的声明*///格式: 委托名 委托对象;//委托链的程序执行是按照委托的先后顺序执行的
0 0
原创粉丝点击