C#:study(17)--代理、事件和创建转换运算符
- 代理(delegate)--可以调用对象的实例化方法和类的静态方法。
格式:delegate ret-type name(parameter-list)
1using System;
2delegate string strMod(string str);
3class DelegateTest
4{
5static string replaceSpaces(string a)
6{
7Console.WriteLine("Replace spaces with hyphens.");
8return a.Replace(" ","-");
9}
10}
11
12public static void Main()
13{
14strMod strOp = new strMod(replaceSpaces);
15string str;
16str = strOp("Test is a test.");
17Console.WriteLine("Result string: " + str);
18}
支持多播的代理〔ret-type必须是void〕
1using System;
2delegate void strMod(ref string str);
3class StringOps
4{
5//用连字符替换空格
6static void replaceSpaces(ref string a)
7{
8Console.WriteLine("Replace spaces with hyphens.");
9a = a.Replace(' ','-');
10}
11//删除空格
12static void removeSpaces(ref string a)
13{
14string temp ="";
15Console.WriteLine("Removing Spaces.");
16for(int i = 0;i<a.Length;i++)
17 if(a[i]!=' ') temp+=a[i];
18a = temp;
19}
20//字符串反序
21static void reverse(ref string a)
22{
23string temp = "";
24int i;
25Console.WriteLine("Reversing string.");
26for(i=a.Length-1;i>=0;i--)
27 temp+=a[i];
28a = temp;
29}
30//Main()
31public static void Main()
32{
33strMod strOp;
34strMod replaceSp = new strMod(replaceSpaces);
35strMod removeSp = new strMod(removeSpace);
36strMod reverseSre = new strMod(reverse);
37string str = "Test is a test";
38//建立多播
39strOp = replaceSp;
40strOp+=reverseStr;
41//调用多播
42strOp(ref str);
43Console.WriteLine("Resulting string: "+str);
44//删除方法和添加方法
45strOp -= replaceSp;
46strOp+=removeSp;
47string str = "Test is a test";
48//调用多播
49strOp(ref str);
50Console.WriteLine("Resulting string: "+str);
51}
52}
- 事件
事件是C#中建立在代理基础上的另一个重要特性。事件是类成员,并以关键字event声明,一般格式如下:enent enent-delegate event-name;
1//多播事件例子
2using System;
3//为事件声明一个代理
4delegate void MyEventHandler();
5//声明事件类
6class MyEvent
7{
8public event MyEventHandler activate;//声明事件
9//调用此方法来触发事件
10public void fire()
11{
12if(activate != null)
13activate();
14}
15}
16class X
17{
18public void Xhandler()
19{
20Console.WriteLine("Event received by X object.");
21}
22}
23class Y
24{
25public void Yhandler()
26{
27Console.WriteLine("Event received by Y object.");
28}
29}
30class EventDemo
31{
32static void handler()
33{
34Console.WriteLine("Event received by EnentDemo.");
35}
36public static void Main()
37{
38MyEvent evt = new MyEvent();
39X xOb = new X();
40Y yOb = new Y();
41//把事件添加到事件链表中
42evt.activate += new MyEventHandler(handler);
43evt.activate += new MyEventHandler(xOb.Xhandler);
44evt.activate += new MyEventHandler(yOb.Yhandler);
45//触发事件
46evt.fire();
47Console.WriteLine();
48//删除事件处理方法
49evt.activate -= new MyEventHandler(xOb.Xhandler);
50evt.fire();
51}
52}
53
54//程序输出如下:
55Event received by EnentDemo.
56Event received by X object.
57Event received by Y object.
58
59Event received by EnentDemo.
60Event received by Y object.
if(activate != null)
activate();
- 创建转换运算符
〔隐式转换〕public static implicit target-type(source-type){return vlaue;}
〔显示转换〕public static explicit target-type(source-type){return vlaue;}
1 //从ThreeD到int的隐式转换运算符
2 public static implicit operator int(ThreeD op)
3 {
4 return op.x*op.y*op.z;
5 }
6 //调用
7 int a;
8 ThreeD zhh = new ThreeD(10,10,10);
9 a = zhh;
10 a = zhh*2;
11 //从ThreeD到int的显式转换运算符
12 public static explicit operator int(ThreeD op)
13 {
14 return op.x*op.y*op.z;
15 }
16 //调用
17 int a;
18 ThreeD zhh = new ThreeD(10,10,10);
19 a = (int)zhh;
20 a = (int)zhh*2;