An example of an incident and a delegate in .NET.

zhaozj2021-02-16  66

For example, a company (scene), you are a boss, there are two employees, little excitement and little king. You ordered Xiao Wang, if you play games, then the little king is buckle to 500 yuan.

This is the commission in reality.

In fact, in the write program, the programmer is the boss, Xiaoshu and Xiao Wang are two objects. Small play game is a method, Xiao Zhang also has a game event, he plays games to stimulate this event. Xiao Wang is an event handling object, he is responsible for deducting small money to 500.

Therefore, it is a few elements: 1 Exciting events - is the object of Xiao Zhang 2 to handle object events - that is the king 3 definition commission, that is, you let Xiao Wang monitor small.

If these three elements are satisfied, you have written a complete event.

There is an example below: Successful running in vs.net2003 C # console application: use system;

Namespace Csharpconsole {public class scene {[stathread] public static void main (string [] args) {console.writeline ("Scene starts ..."); // Generate Xiao Wang Xiao W = New Xiao Wang () ; // Generate small account small Z = New small sheet ();

// Specify monitor Z.PlayGame = new playgamehandler (w. Button money); // Start playing game Z. Play game ();

Console.Writeline ("Scene end ..."); console.readline ();}}

// People responsible for dash money public class king {public king () {console.writeline ("Generated Xiao Wang ...");}

Public Void Deduction (Object Sender, Eventargs E) {Console.Writeline ("Xiao Wang: Good kid, working hours Dare to play games ..."); console.writeline ("Xiao Wang: See how much your kid is. .. "); Small f = (small sheet) sender; console.writeline (" Small money: " f. Money .Tostring ()); console.writeline (" Start to deduct money ... "); System.threading.thread.sleep (500); f. Money = f. Money - 500; console.writeline (" After the deduction .... Now Xiao Zhang still: " f. Money. Tostring );

// If you play the game, you will trigger the event public class minus {// first define an event, this event means "small" in playing games. Public Event PlayGameHandler PlayGame; // Save Small Money Variables Private Int M 00Ey;

Public small () {console.writeline ("Generate Small ...."); M_Money = 1000; // Constructing function, initializing small money. }

Public int money // This property can operate small money. {GET {RETURN M_MONEY;} set {m_money = value;}}

Public void play game () {console.writeline ("Xiao Zhang started to play games ....."); console.writeline ("Small Zhang: CS fun, hahaha! I play ....."); System.Threading.Thread.Sleep (500); System.EventArgs e = new EventArgs (); OnPlayGame (e);} protected virtual void OnPlayGame (EventArgs e) {if (! PlayGame = null) {PlayGame (this, e) }}}

// Define the delegation handler public Delegate Void PlayGameHandler (Object Sender, System.EventArgs E);

}

Among them: Deducting money incident: small sheet f = (small sheet) Sender;

This code indicates the money that makes the king to deduct the little, then when the king is buckled, it is necessary to operate the object instance. If you carefully see the code, you will understand that in the method of the small king's money, there is an Object Sender object in the Oblic Void button, this object represents an object of the event, in this program Inside, this Sender is actually small, but the type of object that is passed is Object, so the object type conversion statement must be used to turn Sender from Object to small sheets, so there is: Xiao Zhang F = (small sheet) Sender;

System.threading.thread.sleep (500); this code is to pause half a second, 500 units are milliseconds, 500 milliseconds are half seconds.

In small sheets, there is a line of code Protected Virtual Void OnPlayGame (Eventargs E) means that this PlayGame event has been registered by other objects. Because small-clad PlayGame events may have no other class notifications to be handled, then this code: if (PlayGame! = NULL) detects that there is no object to send event notifications, otherwise it means that there is already an object needs to process this event, send event notification Let the object processed.

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

New Post(0)