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




浙公网安备 33010602011771号