Delegate, Method as Parameter.
Posted 许明会的计算机技术主页
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delegate, Method as Parameter.相关的知识,希望对你有一定的参考价值。
- 代理, 将方法作为另一方法的参数. 类似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 (); } } }
- 将方法绑定到委托
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 (); } } }
- 委托与接口
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 (); } } }
- 更好的封装,事件的由来
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 (); } } }
- 更好的封装
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 (); } } }
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 (); } } }
的
以上是关于Delegate, Method as Parameter.的主要内容,如果未能解决你的问题,请参考以下文章