Event learning

xiaoxiao2021-03-06  42

The following code is someone else's example. Excerpt. Little rain second school.

When I saw the admission.

If we go out. Which is not required when you use it,

Qinshihuang.eatevent = new EateventHandler (Lishi.arrangeFood);

It is also possible.

// trigger an event

OneArtEvent (e);

This function can also be omitted. Use Eatevent (this, e) directly; this is also omitted

Public Delegate Void EateventHndler (Object Sender, EateventAntargs E);

This definition can also be the case. You can define a parameter directly.

Public Delegate Void EateventHndLer (String E); But when you use the parameters should be good.

Using system;

Namespace NSEventsample

{

///

/// class EateventArgs must inherit the self-class Eventargs to cause incident package data

///

Public Class EateventArgs: Eventargs

{

Public String RestrauntName; // Hotel Name

Public Decimal Moneyout; // Prepare the consumption amount

}

///

/// This delegate uses the method of the method of processing the eating event (mode)

///

Public Delegate Void EateventHndler (Object Sender, EateventAntargs E);

///

/// Turning an Eateevent class master (master), this class must

/// 1. Declare an event called Eatevent: Public Event Eateventhandler Eatevent;

/// 2. Through a method called OneArtEvent, a meal event is triggered, and the data to which the event is handled;

/// 3. Explain that the event is triggered in some case? At hungry. Use method hungrg to simulate.

///

Public Class Master

{

// Declaration event

Public Event Eateventhandler Eatevent;

/ / Method for triggering events

Public Void OneArt (Eateventargs E)

{

IF (Eatevent! = NULL)

{

Eatevent (this, e);

}

}

// When the master is hungry, he will designate the place of eating and consumption.

Public void Hungry (String Restrauntname, Decimal Moneyout)

{

Eateventargs E = New Eateventargs ();

E.RESTRAUNTNAME = RestrauntName;

E.MONEYOUT = Moneyout;

Console.writeline ("The owner said:");

Console.writeline ("I am hungry, I want to go to {0} to eat, consumption {1} yuan", E.Restrauntname, E.Moneyout;

// trigger an event

OneArtEvent (e);

}

}

///

/// Class Servant (servant) has a method arrangefood to deal with the owner's food event

///

Public Class Servant

{

Public void Arrangefood (Object Sender, EateventAndargs E) {

Console.writeLine ();

Console.writeline ("The servant said:");

Console.writeline ("My master, your command is:");

Console.WriteLine ("Eating Location - {0}", E.RESTRAUNTNAME);

Console.WriteLine ("Prepare for Consumption - {0} Yuan", E.MONEYOUT);

Console.writeline ("Ok, arrange it ...");

Console.Writeline ("Master, your food here, please slow");

}

}

///

The servant of Qinshihuang (Qin Shihuang) is Lisi (Li S), and let Li Si's ArrangeFood

/// method to handle Qinshihuang's eating event: qinshihuang.eatevent = new EateventHandler (Lishi.Arrangefood);

///

Public class god

{

Public static void main ()

{

Master qinshihuang = new master ();

Servant Lishi = New Servant ();

Qinshihuang.eatevent = new EateventHandler (Lishi.arrangeFood);

// Qin Shihuang is hungry, I want to go to Hilton Hotel, spending 5,000 yuan

Qinshihuang.hungry ("Hilton Hotel", 5000.0m);

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-56077.html

New Post(0)