zoukankan      html  css  js  c++  java
  • delegate

    一,委托的使用基本步骤:
    1,声明委托类型  public delegate void MyDelegate(string name ,int age);

    2,创建一个委托,指引方法到委托  MyDelegate handler=new MyDelegate(MehtodOfMyDelegate);// 不用构造函数的 MyDelegate handler= MehtodOfMyDelegate;

    3,调用委托(如果函数) handler("yindongli",25);


    二,异步回调(将委托作为参数传递)
    基本步骤同上,
    如:
    public void MethodWithCallback(string name,int age,MyDelegate callback)
    {
        callback(name,age);
    }
    异步回调使用
    MethodWithCallback("dongli",35,handler);


    详细代码:
        class Program
        {
            public delegate void userEventHandler(string name,int age);
            static void Main(string[] args)
            {
                Program p = new Program();
                userEventHandler handler = new userEventHandler(p.ShowUser);//或者 =ShowUser;
                handler("dongli",234);
                //调用异步回调函数

                p.MethodWithCallback("dongli",23,handler);
            }
            public void ShowUser(string name, int age)
            {
                Console.WriteLine("UserName:{0},Age:{1}",name,age);
            }

            //异步回调  函数  ---将委托作为参数
            public void MethodWithCallback(string name, int age, userEventHandler callback)
            {
                callback(name,age);
            }
        }

    三,多路广播
    用(+,+=,-,-=)为委托连续添加处理方法

  • 相关阅读:
    Appium+python自动化2-环境搭建(下)【转载】
    Appium+python自动化1-环境搭建(上)【转载】
    python+requests接口自动化完整项目设计源码【转载】
    python接口自动化10-token登录【转载】
    linux_samba服务搭建
    linux_nginx反向代理
    linux_Nginx优化
    linux_mysql安装
    linux_http协议
    linux_nginx_rewrite
  • 原文地址:https://www.cnblogs.com/lovey/p/2260072.html
Copyright © 2011-2022 走看看