Delegate, Method as Parameter.

Posted 许明会的计算机技术主页

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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框架的委托和事件

以上是关于Delegate, Method as Parameter.的主要内容,如果未能解决你的问题,请参考以下文章

react: typescript system params method optimize

Groovy闭包中的this,owner和delegate

简易delegate委托

关于C#中delegate的用法

java中Method类有啥作用

节流和防抖函数