标签:
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 委托 8 { 9 static class Program 10 { 11 //定义委托 12 delegate void AnimalPlay(string name); 13 14 static void DogPlay(string name) 15 { 16 Console.WriteLine("Name:" + name); 17 Console.WriteLine("汪汪~~~"); 18 } 19 20 static void CatPlay(string name) 21 { 22 Console.WriteLine("Name:" + name); 23 Console.WriteLine("喵喵~~~"); 24 } 25 26 static void LionPlay(string name) 27 { 28 Console.WriteLine(" Name:" + name); 29 Console.WriteLine("呜啊~~~"); 30 } 31 32 /// <summary> 33 /// 使用委托 34 /// </summary> 35 /// <param name="animalPlay">传递的是一个方法</param> 36 /// <param name="name">传递的是要用的名字</param> 37 static void RunCircus(AnimalPlay animalPlay, string name) 38 { 39 animalPlay(name); 40 } 41 42 static void Main(string[] args) 43 { 44 //声明委托实例 45 AnimalPlay a1 = new AnimalPlay(DogPlay); 46 AnimalPlay a2 = new AnimalPlay(CatPlay); 47 AnimalPlay a3 = new AnimalPlay(LionPlay); 48 49 //狗 50 Program.RunCircus(a1, "aaa"); 51 52 //猫 53 Program.RunCircus(a2, "bbb"); 54 55 //狮子 56 Program.RunCircus(a3, "ccc"); 57 58 //*******************************************************// 59 60 //在新版本的.NET中可以不声明委托实例 61 //狗 62 Program.RunCircus(a1, "aaa"); 63 64 //猫 65 Program.RunCircus(a2, "bbb"); 66 67 //狮子 68 Program.RunCircus(a3, "ccc"); 69 70 Console.ReadKey(); 71 } 72 } 73 }
标签:
原文地址:http://www.cnblogs.com/KTblog/p/4525896.html