0%

C#事件与委托

参考HeadFirstC#

事件

当一个事件发生后,不同的事件监视者完成各自的工作

1、定义产生事件的对象
2、定义事件
3、事件发生
4、不同对象订购(绑定)事件

5、创建事件执行需要的参数对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//定义事件及对象
Ball ball = new Ball();
//绑定
Fan fan = new Fan(ball);//Fan对Ball的处理,Fan先绑定BallInPlay
Pitcher pitcher = new Pitcher(ball);//Pitcher对Ball的处理,Pitcher后绑定BallInPlay
BallEventArgs ballEventArgs = new BallEventArgs(60,80);
//事件发生后Fan先响应,Pitcher后响应
ball.OnBallInPlay(ballEventArgs)

//1、定义产生事件的对象
class Ball{
//2、定义事件
//无事件绑定对象时BallInPlay = null
//事件执行顺序为事件绑定顺序
public event EventHandler BallInPlay;
protected void OnBallInPlay(BallEventArgs e){
//EventHandler ballInplay = BallInPlay;
//3、事件发生
//事件发生后会执行BallInPlay的委托,具体的执行发放为事件监视者所委托的方法
BallInPlay(this, e);
//if (ballInplay != null)
// ballInplay(this,e);
}
}
class Pitcher{
public Pitcher(Ball ball){
//4、不同对象订购(绑定)事件
//当事件发生后执行ball_BallInPlay1方法
ball.BallInPlay += ball_BallInPlay1;
}
void ball_BallInPlay1(object sender, EventArgs e){
if (e is BallEventArgs){
BallEventArgs ballEventArgs = e as BallEventArgs;
if (ballEventArgs.Distance < 95 && ballEventArgs.Traj < 60){
CatchBall();
}else
GameOver();
}
}
void CatchBall(){ }
void GameOver(){ }
}
class Fan{
public Fan(Ball ball){
//4、不同对象订购(绑定)事件
//当事件发生后执行ball_BallInPlay2方法
ball.BallInPlay += new EventHandler(ball_BallInPlay2);
}
void ball_BallInPlay2(object sender, EventArgs e){
if (e is BallEventArgs){
BallEventArgs ballEventArgs = e as BallEventArgs;
if (ballEventArgs.Distance > 400)
CatchBall();
else
Screm();
}
}
void CatchBall(){ }
void Screm(){ }
}
//事件执行的参数
class BallEventArgs : EventArgs{
public int Traj { get; private set; }//击球角度
public int Distance { get; private set; }//飞行距离
public BallEventArgs(int traj, int distance){
this.Traj = traj;
this.Distance = distance;
}
}

委托事件

委托

再讲委托

1
2
3
4
5
6
7
8
9
10
11
public delegate string Calculate(double x, double y);
Calculate calculateString = new Calculate(add);
string add = calculateString(1, 1);
calculateString = new Calculate(mul);
string mul = calculateString(1, 1);
public string add(double x, double y){
return (x + y).ToString()
}
public string mul(double x, double y){
return (x * y).ToString()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
delegate void BatCallback(BallEventArgs e);
Ball ball = new Ball();
//创建一个新的Bat对象,使用BatCallback委托将Ball的OnBallInPlay方法引用传给Bat对象
Bat bat = ball.GetNewBat();
Fan fan = new Fan(ball);
Pitcher pitcher = new Pitcher(ball);
BallEventArgs ballEventArgs = new BallEventArgs(60,80);
bat.HitTheBall(ballEventArgs);
class Ball{
//2、定义事件
//无事件绑定对象时BallInPlay = null
//事件执行顺序为事件绑定顺序
public event EventHandler BallInPlay;
protected void OnBallInPlay(BallEventArgs e){
//EventHandler ballInplay = BallInPlay;
//3、事件发生
//事件发生后会执行BallInPlay的委托,具体的执行发放为事件监视者所委托的方法
BallInPlay(this, e);
//if (ballInplay != null)
// ballInplay(this,e);
}
public Bat GetNewBat(){
//将事件处理委托到击球事件中
return new Bat(new BatCallback(OnBallInPlay));
}
}
class Bat{
private BatCallback hitBallCallback;
public Bat(BatCallback callbackDelegate) {
//私有化事件发生后的时间处理
//不使用 += 是为了保证击球手只监视场内的一个Ball
this.hitBallCallback = new BatCallback(callbackDelegate);
}
//发生击球事件后,回调Ball类中的OnBallInPlay响应
public void HitTheBall(BallEventArgs e){
if (hitBallCallback != null)
hitBallCallback(e);
}
}

回调和事件

代码上的区别:
事件发生后的响应在当前类
回调响应方法在其他类