C# 委托与事件

2021-06-16 08:04

阅读:665

标签:动态   技术   结束   hand   cas   key   ted   start   下载   

 委托,相当于函数指针,是引用类型,有多播功能

委托的声明    public delegate double MyDelegate ( double x );

委托的实例化   MyDelegated d2 = new MyDelegate( obj.myMethod );

委托的调用    委托变量名(参数列表 )    d2(8.9) 

委托的合并    多播MultiCastDelegate

  一个委托实例中可以“包含”多个函数

  调用委托,就是调用其中多个函数

  多个函数间的先后顺序是没有意义的

  返回值也就没有太多意义

运算符 + - += -=

  动态地增减其中的函数

  提高了程序的灵活性

 

技术图片技术图片
 1 using System;
 2 
 3 public delegate void Action();
 4 
 5 class Program
 6 {
 7     public static void Main(string[] args)
 8     {
 9         //实例化对象
10         Mom mom = new Mom();
11         Dad dad = new Dad();
12         Child child = new Child();
13         
14         //将爸爸和孩子的Eat方法注册到妈妈的Eat事件
15         //订阅妈妈开饭的消息
16         mom.Eat += dad.Eat;
17         mom.Eat += child.Eat;
18         
19         //调用妈妈的Cook事件
20         mom.Cook();
21         
22         Console.Write("Press any key to continue . . . ");
23         Console.ReadKey(true);
24     }
25 }
26 
27 public class Mom
28 {
29     //定义Eat事件,用于发布吃饭消息
30     public event Action Eat;
31     
32     public void Cook()
33     {
34         Console.WriteLine("妈妈 : 饭好了");
35         //饭好了,发布吃饭消息!!!
36         Eat();
37         //Eat?.Invoke();
38         //(Eat).Invoke();
39     }
40 }
41 
42 public class Dad
43 {
44     public void Eat()
45     {
46         //爸爸去吃饭
47         Console.WriteLine("爸爸 : 吃饭了。");
48     }
49 }
50 
51 public class Child
52 {
53     public void Eat()
54     {
55         //熊孩子LOL呢,打完再吃
56         Console.WriteLine("孩子 : 打完这局再吃。");
57     }
58 }
委托与事件代码

 

技术图片技术图片
  1 using System;
  2 
  3  
  4 
  5 public delegate void DownloadStartHandler(object sender, DownloadStartEventArgs e);  //声明委托
  6 
  7 public delegate void DownloadEndHandler(object sender, DownloadEndEventArgs e);  
  8 
  9 public delegate void DownloadingHandler(object sender, DownloadingEventArgs e);  
 10 
 11  
 12 
 13 public class DownloadStartEventArgs
 14 
 15 {
 16 
 17     public string Url{ get{ return _url;} set{ _url=value;} }
 18 
 19     private string _url;
 20 
 21  
 22 
 23     public DownloadStartEventArgs( string url ){ this._url = url; }
 24 
 25 }
 26 
 27  
 28 
 29 public class DownloadEndEventArgs
 30 
 31 {
 32 
 33     public string Url{ get{ return _url;} set{ _url=value;} }
 34 
 35     private string _url;
 36 
 37  
 38 
 39     public long ByteCount { get { return _byteCount; } set{ _byteCount=value;} }
 40 
 41     private long _byteCount;
 42 
 43  
 44 
 45     public DownloadEndEventArgs( string url, long size  ){ this._url = url; this._byteCount=size; }
 46 
 47 }
 48 
 49  
 50 
 51 public class DownloadingEventArgs
 52 
 53 {
 54 
 55     public string Url{ get{ return _url;} set{ _url=value;} }
 56 
 57     private string _url;
 58 
 59  
 60 
 61     public double Percent { get { return _percent; } set{ _percent=value;} }
 62 
 63     private double _percent;
 64 
 65  
 66 
 67     public DownloadingEventArgs( string url, double percent  ){ this._url = url; this._percent=percent; }
 68 
 69 }
 70 
 71  
 72 
 73  
 74 
 75 public class Crawler
 76 
 77 {
 78 
 79  
 80 
 81     public event DownloadStartHandler DownloadStart;  // 声明事件
 82 
 83     public event DownloadEndHandler DownloadEnd;  // 声明事件
 84 
 85     public event DownloadingHandler Downloading;  // 声明事件
 86 
 87  
 88 
 89     public string Name { get{return name;} set{ name=value;} }
 90 
 91     private string name;
 92 
 93     private string site;
 94 
 95  
 96 
 97      
 98 
 99     public Crawler( string name, string site )
100 
101     {
102 
103         this.name = name;
104 
105         this.site = site;
106 
107     }
108 
109  
110 
111  
112 
113     public void Craw()
114 
115     {   
116 
117         while( true )
118 
119         {
120 
121             string url = GetNextUrl();
122 
123             if( url == null ) break;
124 
125             long size = GetSizeOfUrl( url );
126 
127  
128 
129             if( DownloadStart != null ) //下载开始的事件发生
130 
131             {
132 
133                 DownloadStart( this, new DownloadStartEventArgs(url));
134 
135             }
136 
137              
138 
139             for( long i=0; i1024; i+=1024 )
140 
141             {
142 
143                 //下载数据。。。
144 
145                 System.Threading.Thread.Sleep( 100 );
146 
147                 double percent = (int)(i*100.0/size);
148 
149                 if( percent>100) percent=100;
150 
151                  
152 
153                 if( Downloading != null ) //下载数据的事件发生
154 
155                 {
156 
157                     Downloading( this, new DownloadingEventArgs(url, percent));
158 
159                 }
160 
161             }
162 
163  
164 
165             if( DownloadEnd != null ) //下载结束的事件发生
166 
167             {
168 
169                 DownloadEnd( this, new DownloadEndEventArgs(url, size));
170 
171             }
172 
173         }
174 
175  
176 
177     }
178 
179     private string GetNextUrl()
180 
181     {
182 
183         int a = rnd.Next(10);
184 
185         if( a == 0 ) return null;
186 
187         return site + "/Page" + a + ".htm";
188 
189     }
190 
191     private long GetSizeOfUrl( string url)
192 
193     {
194 
195         return rnd.Next(3000 * url.Length);
196 
197     }
198 
199     private Random rnd = new Random();
200 
201 }
202 
203  
204 
205 class Test
206 
207 {
208 
209     static void Main()
210 
211     {
212 
213         Crawler crawler = new Crawler("Crawer101", "http://www.pku.edu.cn");
214 
215  
216 
217         crawler.DownloadStart += new DownloadStartHandler( ShowStart ); //注册事件
218 
219         crawler.DownloadEnd += new DownloadEndHandler( ShowEnd );
220 
221         crawler.Downloading += new DownloadingHandler( ShowPercent );
222 
223          
224 
225          
226 
227         crawler.Craw();
228 
229     }
230 
231  
232 
233     static void ShowStart(object sender, DownloadStartEventArgs e){
234 
235         Console.WriteLine( (sender as Crawler).Name + "开始下载" + e.Url );
236 
237     }
238 
239     static void ShowEnd(object sender, DownloadEndEventArgs e){
240 
241         Console.WriteLine( "\n\r下载" + e.Url + "结束,其下载" + e.ByteCount + "字节" );
242 
243     }
244 
245     static void ShowPercent(object sender, DownloadingEventArgs e){
246 
247         Console.Write( "\r下载" + e.Url + "......." + e.Percent + "%" );        
248 
249     }
250 
251 }
网络爬虫

 

C# 委托与事件

标签:动态   技术   结束   hand   cas   key   ted   start   下载   

原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10349878.html


评论


亲,登录后才可以留言!