[. NET Object-oriented Programming fundamentals ] (22) events
Event is an important part of learning . NET Object-oriented programming, and we have actually used events in many places before learning events, such as the click of a control events, and so on, these are the events that have been defined when the . NET design controls. In addition, we can define events ourselves.
event is actually a message mechanism, of course, when you click on the control, event is a delegate with a special signature. And the event / message mechanism is windows
To make it easier to understand events, we use the previous animal example to illustrate that there are two or three animals, cats ( named Tom), and two mice (Jerry and Jack),when the cat barks, triggers the event (catshout), and then two mice start to flee (mouserun). Before using the code to implement this example, let's take a look at the written definition of the event .
1. What is an event ?
An eventis a special signature delegate that a class or object notifies other classes or objects of what is happening.
2. Statement of Events
Public event delegate type events name ;
An event is declared using the event keyword, and his return class value is a delegate type.
Usually the name of the event, named +event as his name, in the code as far as possible to use canonical naming, increase the readability of the code.
3. Examples of events
Here's an example of the cat and mouse that started this article
First look at the UML diagram
Like the UML class diagram, there aretwo classes of cat (cat) and mouse (Mouse) that contain its members . when the cat barks (catshout) when , Triggering Events (catshoutevent), Event Notification Mouse , then the rats run. (mouserun).
First look at the code for the two classes as follows:
1 classCat2 {3 stringcatname;4 stringCatcolor {Get;Set; }5 PublicCat (stringNamestringcolor)6 {7 This. CatName =name;8Catcolor =color;9 }Ten One Public voidcatshout () A { -Console.WriteLine (catcolor+"the Cat"+catname+"come on, Meow! Meow! Meow! \ n"); - the //trigger event when Cat barks - //when the cat barks, if there is a registration event in the Catshoutevent, the event is executed - if(Catshoutevent! =NULL) - catshoutevent (); + } - + Public Delegate voidCatshouteventhandler (); A at Public EventCatshouteventhandler catshoutevent; - - } - classMouse - { - stringMousename; in stringMousecolor {Get;Set; } - PublicMouse (stringNamestringcolor) to { + This. Mousename =name; - This. Mousecolor =color; the } * $ Public voidMouserun ()Panax Notoginseng { -Console.WriteLine (Mousecolor +"the mouse"+ Mousename +"said: ' The old cat is coming, run! \ "\ n i run!! \ n I'm running!! I'm speeding up and running!!! \ n"); the } +}
The call is as follows:
1Console.WriteLine ("[Scene description]: A Yuemingxingxi midnight, two mice are stealing oil to eat \ n");2Mouse Jerry =NewMouse ("Jerry","White");3Mouse Jack =NewMouse ("Jack","Yellow");4 5 6Console.WriteLine ("[Scene description]: a black cat crept in and came over .");7Cat Tom =NewCat ("Tom","Black");8 9Console.WriteLine ("[Scene description]: In order to safely steal oil, registered a cat called the event \ n");TenTom.catshoutevent + =NewCat.catshouteventhandler (jerry.mouserun); OneTom.catshoutevent + =NewCat.catshouteventhandler (jack.mouserun); A -Console.WriteLine ("[Scene description]: The cat called Three voices \ n"); - tom.catshout (); the - -Console.readkey ();
The results of the operation are as follows:
< to be continued, additional references to events are further explained in the following >
==============================================================================================
Back to Catalog
< If you have Help, remember to click on the recommendation Oh, there is no understanding of the place or write the wrong place, please communicate more >
QQ Group: 467189533
==============================================================================================
[. NET object-oriented programming fundamentals] (22) Event