• Delegate, Method as Parameter.


    1. 代理, 将方法作为另一方法的参数. 类似C里面的函数指针.
      using System;
      using System.Windows.Forms;
      using System.Threading;
      
      namespace Demo
      {
          public delegate void Greeting(string name);
      
          public class Demo
          {
              private static void GreetingEnglish(string name)
              {
                  Console.WriteLine ("Hello,{0}", name);
              }
              private static void GreetingChinese(string name)
              {
                  Console.WriteLine ("你好,{0}", name);
              }
              //委托,将方法作为另一方法的参数.委托可以代表一系列返回值和参数类型与其一致的方法.
              private static void GreetingPeople(string name, Greeting greet)
              {
                  greet (name);
              }
                  
              public static void Main()
              {
                  GreetingPeople ("Jeffrey Richter", GreetingEnglish);
                  GreetingPeople ("刘德华", GreetingChinese);
                  Console.ReadKey ();
              }
          }
      
      }
    2. 将方法绑定到委托
      using System;
      using System.Threading;
      
      namespace Demo
      {
          public delegate void Greeting(string name);
      
          public class Demo
          {
              private static void GreetingEnglish(string name)
              {
                  Console.WriteLine ("Hello,{0}", name);
              }
              private static void GreetingChinese(string name)
              {
                  Console.WriteLine ("你好,{0}", name);
              }
              private static void GreetingFrance(string nom)
              {
                  Console.WriteLine ("Bonjour,{0}", nom);
              }
              //delegate: use the Method name as Parameters.
              private static void GreetingPeople(string name, Greeting greet)
              {
                  greet (name);
              }
                  
              public static void Main()
              {
                  GreetingPeople ("Jeffrey Richter", GreetingEnglish);
                  GreetingPeople ("刘德华", GreetingChinese);
                  //bind Method to Delegate
                  Greeting chinese = GreetingChinese;
                  Greeting english = GreetingEnglish;
                  english += GreetingFrance;
                  chinese ("许明会");
                  english ("Matt Petrek");
                  Console.ReadKey ();
              }
          }
      }
    3. 委托与接口
      using System;
      using System.Threading;
      
      namespace Demo
      {
          interface IGreeting{
              void Greeting(string name);
          };
      
          public class GreetingChinese:IGreeting
          {
              public void Greeting(string name)
              {
                  Console.WriteLine ("你好,{0}", name);
              }
          }    
          public class GreetingEnglish:IGreeting
          {
              public void Greeting(string name)
              {
                  Console.WriteLine ("Hello,{0}", name);
              }
          }
          public class Demo
          {
              private static void Greeting(string name, IGreeting greeting)
              {
                  greeting.Greeting (name);
              }
              public static void Main()
              {
                  IGreeting chinese = new GreetingChinese ();
                  IGreeting english = new GreetingEnglish ();
                  chinese.Greeting ("许明会");
                  english.Greeting ("Jeffrey Richter");
                  Greeting ("Matt Petrik", english);
                  Console.ReadKey ();
              }
          }
      }
    4. 更好的封装,事件的由来
      using System;
      using System.Threading;
      
      namespace Demo
      {
          public delegate void Greet(string name);
      
          public class GreetingManager
          {
              public Greet deleGreet;
              public void Greeting(string name, Greet greet)
              {
                  greet (name);
              }
          }
      
          public class Demo
          {
              private static void GreetingEnglish(string name)
              {
                  Console.WriteLine ("Hello,{0}", name);
              }
              private static void GreetingChinese(string name)
              {
                  Console.WriteLine ("你好,{0}", name);
              }
      
              //Main()
              public static void Main()
              {
                  GreetingManager gm = new GreetingManager ();
                  gm.deleGreet = GreetingChinese;
                  gm.deleGreet += GreetingEnglish ;
                  gm.Greeting ("Matt Petrik",gm.deleGreet);
                  Console.ReadKey ();
              }
          }
      }
    5. 更好的封装,准备引入事件
      using System;
      using System.Threading;
      
      namespace Demo
      {
          public delegate void Greet(string name);
      
          public class GreetingManager
          {
              public Greet deleGreet;
      
              public void Greeting(string name)
              {
                  if (deleGreet!=null) {
                      deleGreet(name);
                  }
              }
          }
      
          public class Demo
          {
              private static void GreetingEnglish(string name)
              {
                  Console.WriteLine ("Hello,{0}", name);
              }
              private static void GreetingChinese(string name)
              {
                  Console.WriteLine ("你好,{0}", name);
              }
      
              //Main()
              public static void Main()
              {
                  GreetingManager gm = new GreetingManager ();
                  gm.deleGreet = GreetingChinese;
                  gm.deleGreet += GreetingEnglish ;
                  gm.Greeting ("Matt Petrik");
                  Console.ReadKey ();
              }
          }
      }
    6. 委托的目的是把它暴露到外面,以让客户端进行方法注册。Event保证在类的内部总是Private,可认为是为Delegate定制的Property。
      using System;
      using System.Threading;
      
      namespace Demo
      {
          public delegate void Greet(string name);
      
          public class GreetingManager
          {
              //public Greet deleGreet;
              public event Greet EventGreet;
      
              public void Greeting(string name)
              {
                  if (EventGreet!=null) 
                      EventGreet(name);
              }
          }
      
          public class Demo
          {
              private static void GreetingEnglish(string name)
              {
                  Console.WriteLine ("Hello,{0}", name);
              }
              private static void GreetingChinese(string name)
              {
                  Console.WriteLine ("你好,{0}", name);
              }
      
              //Main()
              public static void Main()
              {
                  GreetingManager gm = new GreetingManager ();
                  gm.EventGreet += GreetingChinese;
                  gm.EventGreet += GreetingEnglish ;
                  //Greeting call from client, that's not good!
                  gm.Greeting ("Matt Petrik");
                  Console.ReadKey ();
              }
          }
      }
    7. .NET框架的委托和事件

  • 相关阅读:
    集线程池应用、多次HttpWebRequest请求,自动切换账号等等的移动信息查询软件
    PropertyGrid实现文件选择项
    git通过命令忽略指定文件
    【转】一张图解析FastAdmin中的表格列表的功能
    一张图解析FastAdmin中的FormBuilder表单生成器
    1140 In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'football.order.id'; this is incompatible with sql_mode=only_full_group_by
    centeros 安装mysql
    Flask 教程 第二十三章:应用程序编程接口(API)
    Flask 教程 第二十二章:后台作业
    Flask 教程 第二十一章:用户通知
  • 原文地址:https://www.cnblogs.com/flaaash/p/5353515.html
Copyright © 2020-2023  润新知