zoukankan      html  css  js  c++  java
  • 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框架的委托和事件

  • 相关阅读:
    事务隔离级别,数据库存储过程,Mysql视图,Mysql语句
    Spring注解; Spring Bean
    Java Thread Api
    Java 年轻代、年老代、GC
    Java 线程同步方式
    HashMap
    ArrayList
    安装zabbix环境
    线上应用——高内存占用
    Python入门笔记
  • 原文地址:https://www.cnblogs.com/flaaash/p/5353515.html
Copyright © 2011-2022 走看看