C#中使用event实例

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleEvent
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Car car = new Car();
13 People people = new People();
14 car.myEvent+=new Car.Mydelegate(people.Listener);
15 car.go();
16 Console.ReadKey();
17 }
18 }
19
20 public class Car
21 {
22 public delegate void Mydelegate(bool isGo);
23 public event Mydelegate myEvent;
24
25 public void go(){
26 myEvent(true);
27 }
28 public void stop(){
29 myEvent(false);
30 }
31 }
32
33 public class People
34 {
35 public void Listener(bool isGo)
36 {
37 if (isGo==true)
38 {
39 Console.WriteLine("stop");
40 }
41 else
42 {
43 Console.WriteLine("go");
44 }
45 }
46 }
47 }
posted @ 2011-11-09 20:48  丛林听雨  阅读(709)  评论(0编辑  收藏  举报