public Subscriber1(Shape shape) { IDrawingObject d = (IDrawingObject)shape; d.OnDraw += delegate(object sender, EventArgs e) { Console.WriteLine("Sub1 receives the IDrawingObject event."); }; }
static void Main(string[] args) { //Car c1 = new Car("SlugBug", 100, 10); ////c1.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed); //// 当没有参数传入时可以省略掉delegate之后的()部分 //c1.AboutToBlow += delegate(object sender, CarEventArgs e) //{ // Console.WriteLine("{0} says: {1}", sender, e.msg); //}; //// 使用方法组转换,+=直接结合方法名进行操作 //c1.AboutToBlow += CarAboutToBlow; //EventHandler<CarEventArgs> d = new EventHandler<CarEventArgs>(CarExploaded); //c1.Exploded += d; //for (int i = 0; i < 6; i++) //{ // c1.Accelerate(20); //} //c1.Exploded -= CarExploaded; //c1.AboutToBlow -= CarAboutToBlow; //for (int i = 0; i < 6; i++) //{ // c1.Accelerate(20); //} Shape shape = new Shape(); Subscriber1 sub = new Subscriber1(shape); Subscribe2 sub2 = new Subscribe2(shape); shape.Draw(); Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
public Subscribe2(Shape shape) { IShape d = (IShape)shape; d.OnDraw += (sender, e) => { Console.WriteLine("Sub2 receives the IShape event."); }; }