zoukankan      html  css  js  c++  java
  • 委托

    第一节: 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1420141212
    {//在同一命名空间下,创建类    xinde
          public   delegate      int  weituo(int a,int b);    //     这是委托(选择函数的条件)
         class xinde
        {

            public int jia(int aa, int bb) 
            {
                int c=aa+bb;
                return c;
            }
            public int jian(int aa, int bb)
            {
                int c = aa - bb;
                return c;
            }

             public string Returnstring(string a )
            {
                string s = a;
                return s;
            }

        }


    }

    ###############################

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1420141212

    {  class program

    {

    static void Main (string[] args)

    {

                //#########################   这里是委托  以及   应用

                weituo w;                               //定义委托,下一步表示   调用的方法。
                w = new xinde().jia;                    //jia代表类里面的   jia方法
                Console.WriteLine(w(2,3));              //这是 实名委托的应用。

                 Func<string"这里 表示输入的参数类型", string “表示输出的参数类型">,详细情况可以输入Func<>查看详细。  

                Func<string, string>    myfunc = new xinde().Returnstring;
                                                        //Returnstring代表类里面的   Returnstring方法
                Console.WriteLine(myfunc("试试"));      //这是 匿名委托的应用。

                Func<string, string> newfunc = delegate(string s)  //这是新建的委托
                {   return s + "hh";     };    //这是新建的方法
                Console.WriteLine(newfunc("打完"));

                Func<string, int, string> n = (s, a) =>                     //这样也可以(同上)delegate(string s,int a)
                {     return s + "hh"+a ;       };    
                Console.WriteLine(n("打完",999));

                Func<string, int, string> mym = (s, a) => s + "hh" + a;     //这样也可以(同上)," => "  表示“指向”
                Console.WriteLine(mym("打完", 999));

    }

    }

    }

    第二节

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Search : System.Web.UI.UserControl   //自定义用户控件(放了一个TextBox和Button)
    {
        public delegate void ShowDelegate(string s);           //创建  委托类型(输入参数,不输出参数)
        public event ShowDelegate Show;                      
                  //创建  委托   命名show (任何符合输入string值,不输出值的函数,都可show+=函数名 )
        protected void Page_Load(object sender, EventArgs e)
        { }
        protected void btn_Click(object sender, EventArgs e)    //点击触发函数,执行show  对应的函数
        {
            //执行查询。。。。。
            string s = DateTime.Now.ToString();
            //显示。
            if (Show != null)
            {
                Show(s);
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Search1.Show += SetValueToTextBox;
            Search1.Show += ShowTitle;
        }
        private void SetValueToTextBox(string ttt)
        {
            TextBox1.Text = ttt;
        }
        public void ShowTitle(string ssss)
        {
            this.Title = ssss;                   //this   代表网页。
        }
    }

    代理:委托;(代理与类、接口是同一个层次的。代理与类思想上完全一样。)


    指向方法的指针。
    代理与它指向的方法,往往不是一个人写出来的

    做委托的四大步:

    1.声明代理类型
    public delegate void WangYeDelegate(sring s);

    2.定义代理变量
    public [event] WangYeDelegate ZhuanDong;

    3.把代理挂到相应的方法上去。
    xxx.ZhangDong += xxx.yyyyy;

    4.编写代理要调用的方法。


    if(ZhuangDong!=null)
    {
     ZhuangDong();
    }


     

  • 相关阅读:
    linux删除/var/log/下面所有日志 如何重新记录日志
    DIV里的内容自动换行
    it冲突:commit your changes or stash them before you can merge. 解决办法
    git切换到远程分支
    【异常】warning: refname 'feature1.3.0' is ambiguous.导致git merge失败
    在此篇文章中,我们将用 15 分钟对 PHP v7.x 版本更改进行简要回顾
    input元素所有type类型及相关作用
    微信自动关闭内置浏览器页面,返回公众号窗口 WeixinJSBridge.call('closeWindow')
    css背景渐变色
    数组的forEach和map和for方法的区别
  • 原文地址:https://www.cnblogs.com/huaze/p/4357292.html
Copyright © 2011-2022 走看看