About the wonderful explanation of the entrusted

xiaoxiao2021-03-06  109

Author: TomMax (laughs hope life) Source: 9cbs community

Theme: ".net 100 Questions Q & A activity" - a lot of people asked - "C # delegate and event" Author: TomMax (laughs hope life) credit value: 100 Quantity forum: .NET C # technical problems Points: 20 responses Time: 62 Posted: 2004-07-13 07:52:33 There are many people asking, .NET delegate 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 Dedicated money); // Start playing game Z. Play game (); console.writeline ("Scene end ..."); console.readline ();}} // Responsible for dash money public class king { Public King () {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 button!"); 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 small {// first define an event, this event representation "Small Zhang" is playing games. Public Event PlayGameHandler PlayGame; // Save Small Money Variable PRIVATE INT M_MONEY; PUBLIC Small () {Console.WriteLine ("Generate Small ...."); m_money = 1000; // Constructor, Initialization Small Money. } Public int money // This property can operate the little money.

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

New Post(0)