C# 委托与事件
2021-06-16 08:04
标签:动态 技术 结束 hand cas key ted start 下载 委托,相当于函数指针,是引用类型,有多播功能 委托的声明 public delegate double MyDelegate ( double x ); 委托的实例化 MyDelegated d2 = new MyDelegate( obj.myMethod ); 委托的调用 委托变量名(参数列表 ) d2(8.9) 委托的合并 多播MultiCastDelegate 一个委托实例中可以“包含”多个函数 调用委托,就是调用其中多个函数 多个函数间的先后顺序是没有意义的 返回值也就没有太多意义 运算符 + - += -= 动态地增减其中的函数 提高了程序的灵活性 C# 委托与事件 标签:动态 技术 结束 hand cas key ted start 下载 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10349878.html 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; i