委托

 1 namespace ConsoleApplication4
 2 {
 3     class folder
 4     {
 5         public static void StopFolding()
 6         {
 7             Console.WriteLine("hell world");
 8         }
 9     }
10    
11     delegate void stopMachineDelegate(); 
12  
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             stopMachineDelegate a = new stopMachineDelegate(folder.StopFolding);
18             a();
19         }
20     }
21 }
22  
23  
24 ********************************************************************************
25  
26 namespace ConsoleApplication4
27 {
28     class folder
29     {
30         public static void StopFolding()
31         {
32             Console.WriteLine("hell world");
33         }
34         public static void StopFlding()
35         {
36             Console.WriteLine("helrld");
37         }
38  
39  
40     }
41     class Control
42     {
43         public delegate void stopMachineDelegate();
44         public stopMachineDelegate stopMachinery;
45         public  Control()
46         {
47             this.stopMachinery += folder.StopFolding;
48             this.stopMachinery += folder.StopFlding;   //output" hell world helrld"
49  
50             //this.stopMachinery = new stopMachineDelegate(folder.StopFolding);
51         }   
52         public void shutdown()
53         {
54             if(stopMachinery != null)
55                 this.stopMachinery();
56         }     
57     }
58  
59  
60     class Program
61     {
62         static void Main(string[] args)
63         {
64             Control c = new Control();
65             c.shutdown();
66         }
67     }
68 }

 

posted @ 2018-01-20 21:55  奔跑的蒲公英  阅读(124)  评论(0编辑  收藏  举报