Introduction to the event processing mechanism of Java and C #

Source: Internet
Author: User
Introduction to the event processing mechanism of Java and C #-general Linux technology-Linux programming and kernel information. The following is a detailed description. The preceding two methods are named synchronized, because when running in a multi-threaded environment, several objects may need to be registered and logged out at the same time, and synchronized is used to ensure synchronization between them. Development tools or programmers use these two methods
Create an event stream between the source and listener.

Protected void policymodelchanged (){

/* The Event source uses this method to notify the listener of A modelChanged event */


Vector l;


EventObject e = new EventObject (this );


   

First, copy the listener to the l Array and freeze the EventListeners status to pass events. This ensures that the corresponding method of the target listener who has received the event does not take effect until the event is passed to all listeners.

Synchronized (this ){


L = (Vector) listeners. clone ();


}


For (int I = 0; I <l. size (); I ++ ){


/* Notify each listener registered in the listener queue in sequence that the modelChanged event occurs,


The event status object e is passed as a parameter to each listener in the listener queue */


(ModelChangedListener) l. elementAt (I). modelChanged (e );


}


}


}



In the program, we can see that the event source Model class explicitly calls the modelChanged method in the interface. Actually, the event state Object e is used as a parameter and passed to the modelChanged method in the listener class.

Adaption class

Adaptation classes are an extremely important part of the Java event model. In some applications, events are transmitted from the source to the listener through the Adaptation class ". For example, when the event SOURCE sends an event, and several event listening objects can receive the event, but only the specified object responds to the event, you need to insert an event adapter class between the event source and the event listener to specify which listeners should respond to the event. The adaptation class becomes the event listener. The event source actually registers the adaptation class as the listener into the listener queue, but the real event responder is not in the listener queue, the action that the event responder should do is determined by the adaptation class. Currently, most development tools use adaptive classes to process events when generating code.

C # event processing

In. NET application development, whether it is WEB Forms (ASP. NET) or Windows Forms, all involve the Event Response and processing of a large number of objects, such as the customer submitting an order online, or moving the mouse over the Windows window. In C #, how does one declare an event and add a response method to the event?

In C #, the Events member is used to declare a class event. The following syntax is used to declare an event member in a class:

Public event indicates the name of the event.

For example, if a Click event member is declared in the Control class, its syntax is as follows:

Public event EventHandler Click;

In C #, a new data type delegate (representative) is added to solve the event processing problem. It indicates that the data type is very similar to the pointer in C language. What is different from the pointer is that the Code is secure and manageable. Due to the simplicity of C # itself, it is very easy to understand delegate for programs that have never used C and pointers.

In C #, by using delegate, you can easily use the "+ =" (plus or equal) operator. add one or more response methods to an event in the. Net object. You can also cancel these response methods by using the very simple "-=" (minus or equal) operator. For example, the following statement adds a Click event for the temp button:

Temp. Click + = new System. EventHandler (this. Test); // Add an event handling method for test.


In the statement that declares the event above, Eventhandler is a delegate (Representative) type, which is declared in the. Net class library as follows:

Public delegate void EventHandler (object sender, EventArgs e );


In this way, all functions, such as void (object parameter name, EventArgs parameter name), can be used as the Click Event Response Method of the Control class. As defined below, an event response method:

Private void button#click (object sender, System. EventArgs e)


Because events are handled by delegate (representing the type), an event may have multiple response methods through accumulation. At the same time, you can also use a method as the response method for multiple events. (Note: Only "+ =" and "-=" can appear after the event member in the C # language class to indicate operators for adding and canceling event Response functions .)

Whether it is ASP. Net or general Windows Forms programming, in C #, basically the event response methods we encounter are described as follows:

Private void button#click (object sender, System. EventArgs e)


Do the access permissions, return value types, parameters, types, and even method names of an event response method remain unchanged? The answer is: no!

Generally, there are two parameters in the event response method, one of which indicates that the event-triggering object is sender. Because the event-triggering object is unpredictable, therefore, we declare it as the object type, and all objects apply. The second parameter indicates the specific information of the event. Different types of events may be different, which is determined by the description of the event members in the class.

We know that events are handled through delegate (representative. Assume that the event representative is described as follows:

Delegate int MyEventHandler (object sender, ToolBarButtonClickEventArgs e );



When the above Event Response Function declaration is involved, it must be declared as follows:

Private int MyTest (object sender, ToolBarButtonClickEventArgs e ){}


You can use the following code to add an event response method to an object:

Control. Event + = new MyEventHandler (MyTest );

In general, Java event processing is more direct and simple. the C # event processing references a proxy, which makes the program more flexible and reflects the loose coupling between programs. stryon announced the implementation of Microsoft on the Java development platform. NET, named iNET. in addition, the Beta3 version of iNET was recently released, including the three-level event processing mechanism of C # implemented using Java.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.