日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />2008-6-11
学习内容:委托
遗留问题:今天的内容比较模糊,需要进一步理解
学习总结:
1.       委托
知识点一:委托声明定义了一种类型,它用一组特定的参数以及返回类型来封装方法。对于静态方法,委托对象封装要调用的方法。对于实例方法,委托对象同时封装一个实例和该实例的一个方法。如果您有一个委托对象和一组适当的参数,则可以用这些参数调用该委托。
知识点二:委托本质是一个类(可以通过查看IL代码证实),他是C++中函数指针的替代品;C++中函数的指针只能指向静态的方法,而在C#中也能指向实例对象。
使用委托的代码实例:
using System;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
delegate void eatMydelegate(string food);
class Mydelegate
{
    static void zsEat(string food)
    {
        Console.WriteLine("张三吃"+food);
    }
    static void lsEat(string food)
    {
        Console.WriteLine("李四吃"+food);
    }
    static void wwEat(string food)
    {
        Console.WriteLine("王五吃"+food);
    }
    static void Main()
    {
        eatMydelegate zs=new eatMydelegate(zsEat);
        eatMydelegate ls=new eatMydelegate(lsEat);
        eatMydelegate ww=new eatMydelegate(wwEat);
        zs("西瓜");
        ls("西瓜");
        ww("西瓜");
       
    }
}
上述代码中三个委托调用同一个参数(西瓜),过于繁琐有没有什么解决办法呢?委托链的引入
using System;
delegate void eatMydelegate(string food);
class Mydelegate
{
    static void zsEat(string food)
    {
        Console.WriteLine("张三吃"+food);
    }
    static void lsEat(string food)
    {
        Console.WriteLine("李四吃"+food);
    }
    static void wwEat(string food)
    {
        Console.WriteLine("王五吃"+food);
    }
    static void Main()
    {
        eatMydelegate zs=new eatMydelegate(zsEat);
        eatMydelegate ls=new eatMydelegate(lsEat);
        eatMydelegate ww=new eatMydelegate(wwEat);
        eatMydelegate eatChain;//定义委托连
        Console.WriteLine("张三,李四,王五,开座谈会");
        eatChain = zs + ls + ww;
        eatChain("西瓜");//给委托链赋参数
        Console.WriteLine("李四出去接电话");
        eatChain -= ls;//c#重载了+=和-=运算符,因此可以创建可变的委托链
        eatChain("香蕉");
        Console.WriteLine("李四回来了");
        eatChain += ls;
        eatChain("橘子");
    }
}
用 .net framework2.0 引入的新特性,匿名方法,来简化上面的代码
using System;
delegate void eatMydelegate(string food);
class Mydelegate
{
    static void Main()
    {
        eatMydelegate eatChain;
        eatChain = null;
        eatChain += delegate(string food) { Console.WriteLine("张三吃" + food); };//.net framework2.0引入的新特性
        eatChain += delegate(string food) { Console.WriteLine("李四吃" + food); };
        eatChain += delegate(string food) { Console.WriteLine("王五吃" + food); };
        eatChain("香蕉");
    }
}
动态方法代理:
using System;
delegate void eatMydelegate(string food);
class Man
{
    private string name;
    public Man(string name)
    {
         this.name = name;
    }
    public void eat(string food)
    {
        Console.WriteLine(name + " 吃" + food);
    }
}
class Party
{
    static void eatTogerther(string food, params eatMydelegate[] values)
    {
        if (values == null)
        {
            Console.WriteLine(" 座谈会结束" );
        }
        else
        {
            eatMydelegate eatChain = null;
            foreach (eatMydelegate ed in values)
                eatChain += ed;
            eatChain(food);
            Console.WriteLine();
        }
    }
    static void Main()
    {
        Man ZS = new Man(" 张三" );
        Man LS = new Man(" 李四" );
        Man WW = new Man(" 王五" );
        eatMydelegate zs = new eatMydelegate(ZS.eat);
        eatMydelegate ls = new eatMydelegate(LS.eat);
        eatMydelegate ww = new eatMydelegate(WW.eat);
        Console.WriteLine(" 张三,李四,王五,开座谈会" );
        eatTogerther(" 西瓜" ,zs,ls,ww);
        Console.WriteLine(" 李四出去接电话" );
        eatTogerther(" 香蕉" ,zs,ww);
        Console.WriteLine(" 李四回来了" );
        eatTogerther(" 橘子" ,zs,ls,ww);
        eatTogerther(null,null);
    }
}

转载于:https://blog.51cto.com/xiaoshu838/89444

看陈广老师c#参考视频总结(第六篇)相关推荐

  1. 看陈广老师c#参考视频总结(第三篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  2. 看陈广老师c#参考视频总结(第十篇 完)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  3. 看陈广老师c#参考视频总结

    http://xiaoshu838.blog.51cto.com/433568/89243 转载于:https://blog.51cto.com/1724802/592450

  4. 看陈广老师c#参考视频总结(第八篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  5. 看陈广老师c#参考视频总结(第二篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  6. 看陈广老师c#参考视频总结(第四篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  7. 陈广老师C#参考视频 方法的参数传递 总结

    方法的参数传递有三种: 1.值参数 方法名称(参数类型,参数名称) 2.引用参数 方法名称(ref 参数类型,参数名称) 3.输出参数 方法名称(out  参数类型,参数名称) 运行结果: i=0 j ...

  8. 陈广老师 C#语言参考视频打包下载地址

    陈广老师 C#语言参考视频下载地址 http://dl.getdropbox.com/u/97203/chenguang.zip 转载于:https://www.cnblogs.com/ycxyyzw ...

  9. C#语言俄罗斯方块源代码(据陈广老师视频)

    以下是我根据陈广老师视频,自己敲的代码,日后会不断更新. using System; using System.Collections.Generic; using System.ComponentM ...

最新文章

  1. 机器学习与高维信息检索 - Note 5 - (深度)前馈神经网络((Deep) Feedforward Neural Networks)及基于CVXOPT的相关实例
  2. linux perl telnet安装,Perl--Net::Telnet模块
  3. at指令 fpga_FPGA毕设系列 | 无线通信
  4. mini mp3模块 输出_小米有品众筹魔方mini电脑主机
  5. 作业09-集合与泛型
  6. 模拟请求分页式存储管理 ---4种置换算法
  7. android中组件获取焦点
  8. 苹果隐私部门负责人:在iPhone上侧载应用会减少用户选择
  9. kafka自带的zk启动_kafka-eagle监控和管理kafka
  10. 施耐德 m340 编程手册_施耐德PLC漏洞历险记
  11. 移动端浏览器监听返回键
  12. idea 导入的模块 右下角没有蓝色方块
  13. iPhone通讯录导入及备份方法
  14. 使用certbot openresty执行获取 Let’s Encrypt https 免费证书
  15. 10 个用于收集硬件信息的 Linux 命令
  16. Oracle入门学习详解
  17. 微信小程序手势图案锁屏、解锁实现并提供onSuccess等回调
  18. 2021年中国房地产行业发展现状分析(附房地产开发投资额、房屋施工面积、商品房销售情况及企业排名统计)[图]
  19. 淘宝API接口,item_cat_get-获得淘宝商品类目
  20. LCD背光驱动 --Backlight

热门文章

  1. 用python爬取东方财富股票
  2. 一文彻底弄懂大端与小端
  3. defunct(DEFUNCTRUEGO)
  4. python runtime错误_python出现RuntimeError错误
  5. 【20200429】编译原理课程课业打卡十九之判断OPG文法求解句子分析过程
  6. 3.开发java的三个步骤
  7. 第六章:Matplotlib之场景案例显神通
  8. flutter生成源代码_Flutter随机迷宫生成和解迷宫小游戏功能的源码
  9. php专业软件是什么意思,软件主要是什么的总称
  10. 如何实现视频的快进快退功能(整理)