Invincible explanation

xiaoxiao2021-03-06  75

There are many people asking, delegates in .NET, and event processing. I have a simple example shows that it is an example in reality: For example, a company (scene), you are a boss, there are two employees, Xiaoshu and Xiao Wang. 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. Here's an example: in the VS.Net2003 C # console application editing operation success: use system; namespace csharpconsole {public class scene {[stathread] public static void main (string [] args) {console.writeline ("Scene started "); // Generate Xiao Wang Xiao W = New Xiao Wang (); // Generate small account small Z = New small (); // Specify monitor Z.PlayGame = new PlayGameHandler (w. Buckle money ); // Start playing game z. Play game (); console.writeline ("Scene"); console.readline ();}} // Responsible for dash money public class Xiao Wang {public Class () { Console.Writeline ("Generated Xiao Wang");} public void, Eventargs E) {Console.WriteLine ("Xiao Wang: Good boy, working hours dare to play games"); console.writeline ("Xiao Wang : See how much your kid is "); small sheet f = (small sheet) sender; console.writeline (" Small money: " f. Money .tostring ()); console.writeline (" Start deduction "); 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.

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

New Post(0)