zoukankan      html  css  js  c++  java
  • 对于委托,我这小菜鸟的理解。

    委托是什么,这个个人有个人的理解方式。

    我的理解方式就是它是一个中间商。

    为什么这么说,待我用代码来解释。

    委托奸商类:

     1         /// <summary>
     2         /// 委托中间人
     3         /// </summary>
     4         public class DeleageteT
     5         {
     6             /// <summary>
     7             /// 定义一个委托,我能做的某一类事。
     8             /// </summary>
     9             /// <param name="text"></param>
    10             public delegate void woshiweituo(string text);
    11 
    12             /// <summary>
    13             /// 定义一个事件,谁要委托我啊!赶紧来!
    14             /// </summary>
    15             public event woshiweituo woshishijian;
    16 
    17             /// <summary>
    18             /// 定义一个开始方法,我可不知道什么时候开始干活,你要告诉哦!
    19             /// </summary>
    20             /// <param name="text"></param>
    21             public void StarDelegate(string text)
    22             {
    23                 if (woshishijian != null)
    24                 {
    25                     //执行注册中的事件。
    26                     woshishijian(text);
    27                 }
    28                 else
    29                 {
    30                     Console.WriteLine("都没注册事件,托毛啊!");
    31                 }
    32             }
    33         }

    一堆委托人:

            /// <summary>
            /// 我是第一个委托人
            /// </summary>
            public class Implementation1
            {
                public void show(string text)
                {
                    Console.WriteLine("{0}{1}", text, this.GetType().Name);
                }
            }
    
            /// <summary>
            /// 我是第二个委托人
            /// </summary>
            public class Implementation2
            {
                public void show(string text)
                {
                    Console.WriteLine("{0}{1}", text, this.GetType().Name);
                }
            }
    
            /// <summary>
            /// 我是第三个委托人
            /// </summary>
            public class Implementation3
            {
                public void show(string text)
                {
                    Console.WriteLine("{0}{1}", text, this.GetType().Name);
                }
            }

    开始啦!

     1         static void Main()
     2         {
     3             //第一个委托人
     4             Implementation1 i1 = new Implementation1();
     5             //第二个委托人
     6             Implementation2 i2 = new Implementation2();
     7             //第三个委托人
     8             Implementation3 i3 = new Implementation3();
     9 
    10             //奸商来了!
    11             DeleageteT delegetes = new DeleageteT();
    12 
    13             //赶紧注册吧!
    14             delegetes.woshishijian += i1.show;
    15             delegetes.woshishijian += i2.show;
    16             delegetes.woshishijian += i3.show;
    17 
    18             //奸商开始干活了
    19             delegetes.StarDelegate("我的名字是:");
    20 
    21             Console.ReadKey();
    22         }

    图:。。。。

    如果没错的话,本例子里面的是多播委托。对于多播委托只能用关键字void。为什么?百度吧。。。。

    众位有什么见解,希望能提出来,我们共同进步,求大神指点。。。。

  • 相关阅读:
    Python深入03 对象的属性
    利用Webkit抓取动态网页和链接
    分享:OCILIB 3.11.0 发布,跨平台 Oracle 驱动
    Knockoutjs实战开发:控制子绑定(control descendant bindings)
    利用InjectedBundle定制自己的Webkit(二)
    使用solrj和EasyNet.Solr进行原子更新
    Chaos网络库(二) Buffer的设计
    分享:djangohaystack+solr实现搜索
    Moon.ORM 4.4 隆重发布,在性能和使用便捷上一挑群雄(mysoft,cyq,pdf)
    数据结构利器之私房STL(中)
  • 原文地址:https://www.cnblogs.com/FlyStupidBird/p/6237291.html
Copyright © 2011-2022 走看看