C# 委托代理静态的方法

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 返回值类型 委托名(参数列表);
/*委托对象的声明*/
//格式: 委托名 委托对象;

//委托链的程序执行是按照委托的先后顺序执行的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值