The image introduces the delegation and event processing in .NET.

zhaozj2021-02-16  83

There are many people asking, delegates in .NET, and event processing. I have explained the simple example, it is an example in reality:

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 starts 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);

}

comment:

Good! For event mechanisms, C # provides the sweetness of the syntax, but it has caused troublesome trouble.

2. Let's think about it. If there is a little Li, if the boss specifies: If the small game is playing game, not only the small king is small 500 yuan, but let Xiao Li Xiao Zhang, how do you write?

3. Thank you for your concern, there are still few points in the program: Delivery incident: small f = (small sheet) sender; // What is this statement? System.threading.thread.sleep (500); // What is the role? Play game event: system.threading.thread.sleep (500); system.eventargs e = new Eventargs (); onplayGame (e); can you explain it?

4. Go back to Tower: 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) This method is to trigger the event method, because the PlayGame event is a small event, then in order to trigger this event, you must specify a method of incident an event, so In the public void playing the game () method in small sheets, there is onplaygame (e);

5. Also, I said above: ----------------- Everyone thinks, if there is a little Li, if the boss specifies: if the small game is playing game, not only Xiao Wang buckle Xiao Zhang 500 yuan, and let Xiao Li Xiao Zhang, how do you write? ----------------- This kind of please consider it, this is actually the principle of the use of the combination of .NET (multiple broadcast entrusted), is when an object has stimulated incident, Two (or more) objects can handle this event in their own way.

6.PUBLIC CLASS Xiao Li {PUBLIC Xiao Li () {Console.WriteLine ("Generated Little Li ...");}

Public void, Eventargs E) {Console.Writeline ("Xiao Li: Good boy, getting dare to play games ..."); console.writeline ("Xiao Li: just me to itch ..." Console.writeline ("Start 揍 小 ... ..."); system.threading.thread.sleep (500); console.writeline ("After you finish, Xiao Zhang now Niroprosis");}}

生 生 李 ();

// Specify monitor Z.PlayGame = new playgameHandler (w. Button); z.PlayGame = new PlayGameHandler (li. 揍人);

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

New Post(0)