zoukankan      html  css  js  c++  java
  • 简单的委托示例

    单委托:

        

     1 using System;
     2 namespace test
     3 {
     4     public delegate void saydel(string name);
     5     class Hello
     6         {
     7             public void sc(string name)
     8             {
     9                 string str="你好,"+name;
    10                 Console.WriteLine(str);
    11             }
    12             public void se(string name)
    13             {
    14                 string str="hello,"+name;
    15                 Console.WriteLine(str);
    16             }
    17             public void DoWork(string name,saydel ms)
    18             {
    19                 ms(name);
    20             }
    21             static void Main()
    22                 {
    23                     Hello h=new Hello();
    24                     h.DoWork("user",h.se);
    25                     h.DoWork("user",h.sc);
    26                 }
    27         }
    28 }
    29 

    多播委托:

     1 using System;
     2 namespace test
     3 {
     4     public delegate void saydel(string name);
     5     class Hello
     6         {
     7             public void sc(string name)
     8             {
     9                 string str="你好,"+name;
    10                 Console.WriteLine(str);
    11             }
    12             public void se(string name)
    13             {
    14                 string str="hello,"+name;
    15                 Console.WriteLine(str);
    16             }
    17             public void DoWork(string name,saydel ms)
    18             {
    19                 ms(name);
    20             }
    21             static void Main()
    22                 {
    23                     Hello h=new Hello();
    24                     saydel deg=h.sc;
    25                     deg+=h.se;
    26                     h.DoWork("user",deg);
    27                 }
    28         }
    29 }
    30 
  • 相关阅读:
    [Luogu] 借教室
    [Luogu] 子共七
    [Luogu] 让我们异或吧
    【bzoj1030】[JSOI2007]文本生成器
    HDU3068 最长回文
    【bzoj2342】[Shoi2011]双倍回文
    【NOIP2012】借教室
    HDU2203 亲和串
    【POJ2001】Shortest Prefixes
    vodevs3031 最富有的人
  • 原文地址:https://www.cnblogs.com/xpxu/p/1701566.html
Copyright © 2011-2022 走看看