Programmer: Java and C # event processing mechanism - 启航
Event processing of Java and C # implements event source-event responder mechanism, but it is not exactly the same. Java implemented an event source and event responder two-level entity object, the event respondents are also event monitors The C # implementation is an event source-agent-event responder three-level entity object mode. The following two modes will be specifically described below.
Java event handling
Conceptually, an event is a transmission mechanism that changes between "source objects" and "listener objects". There are many different purposes, such as mouse events, window boundaries, etc., and keyboard events, etc., for example, in Windows systems. In Java, a general, expandable event mechanism is defined, which provides a public framework for the definition and expansion of the model type and passing model, and is suitable for extensive applications. There is a high integration with Java language and the environment. The event can be described to capture and ignition. Other constructors can take some techniques to directly control events during design, as well as between event sources and event listeners. The event mechanism itself does not rely on complex development tools.
The event from the event source to the listener is conducted by the Java method call to the target listener object. A clear Java method is defined accordingly for each of the clear events. These methods are centralized in the event listener (EventListener interface, this interface should inherit Java.util.EventListener. A class that implements some or all of the methods in an event listener interface is an event listener. With the occurrence of the event, the corresponding state is usually packaged in the event status object, which must inherit from Java.util.EventObject. Event status objects are transmitted to the listener method that should respond to the event as a single ginseng. The identity of the event source that issues a certain particular event is that the design format of the specified design is the registration method for event listeners and accepts references to the specified event listener interface instance. Sometimes, event listeners cannot directly implement event listener interface, or other additionalctions, it is necessary to insert an instance of an event adapter class between a source and other one or more listeners to establish them. Contact. The status information related to the event status object is generally encapsulated in an event status object, which is a subclass of Java.util.EventObject. According to the design habits, the names of this event status object class should be ended at Event. For example: public class MouseMovedExampleEvent extends java.util.EventObject {protected int x, y; / * create a mouse move event MouseMovedExampleEvent * / MouseMovedExampleEvent (java.awt.Component source, Point location) {super (source); x = location. x; y = location.y;} / * Get the mouse position * / public point getLocation () {Return New Point (x, y);} EventListener Interface and event listener due to the Java event model is Based on method call, there is thus a way to define and organize event handling methods. Event manipulation methods are defined in the EventListener interface inherited by the Java.util.EventListener class, and the naming of the EventListener interface is ending with Listener. Any class If you want to manipulate the method defined in the EventListener interface, you must perform this interface. This class is the event monitors. For example: / * Define a mouse mobile event object * / public class mouseMoveDexampleEvent Extends java.util.EventObject {// In this class contains status information related to the mouse mobile event ...} / * Defines the mouse movement Event's listener interface * / interface mousemovedexamplelistener extends java.util.EventListener {/ * Defines how the mouse mobile event listener should support the method * / void mouseMovent MME);} Only the method is defined in the interface Name, method of parameters and return value types. Such as: The specific implementation of the mousemoved method in the above interface is defined in the ArbitraryObject class below.
Class ArbitraryObject Implements MousemovedexampleListener {Public Void MouseMoved (MousemovedexampleEvent MME) {...}} ArbitraryObject is the monitors of the MouseMoveDexampleEvent event. The registration and logout of the event listener For various possible event listeners to register their own events into the appropriate event source, establish the event stream between the source and event listeners, the event source must provide registration and logout for event listeners. This use process has been seen in the previous Bound property introduction, in practice, event listeners registration and logout To use standard design format: public void add
* / Synchronized (this) {l = (vector) listeners.clone ();} for (int i = 0; i In the .NET application development, whether it is Web Forms (ASP.NET) or Windows Forms, all involves the event response and processing of a large number of objects, such as the customer 's online submission, or move the mouse on the Windows window. There will be an event. So, how do you declare an event and add a response method for an event? In C #, events (events) are used to declare a class event. Declaring an event member in a class generally adopted the following grammatical form: public event represents the name of the event name. If a Click event member is declared in the Control class, its syntax is as follows: Public Event athandler click; in C #, a new data type Delegate is added to resolve the event handling problem. The representative data type is very similar to a pointer in the C language, which is different from the pointer, which is a secure, manageable. Due to the simplicity of C # itself, it is also very easy to understand Delegate for programs that have not been used by C and pointers. In C #, by using Delegate, you can add one or even multiple response methods to an event in the .NET object via " =" (add equal) operator; can also be very simple "- =" (Equivalence equivalent) operator cancels these response methods. The statement of the Click event is added as shown below: Temp.Click = New System.EventHandler (THIS.TEST); / / EventHandler is a delegate type, EventHandler is a delegate type, EventHandler is a delegate type. It is declared in the .NET class library: Public Delegate Void EventHandler (Object Sender, Eventargs E); this, all the words such as the Void letter (Object parameter name, Eventargs parameter name); functions can be used as a Control class Click event response method. As defined below: Private Void Button1_Click (Object Sender, System.EventArgs E) Due to the delegate (representative type), it is possible to have multiple response methods by accumulating an event; At the same time, it is also possible to make a method of respond to a plurality of events. (Note: After the EVENT member in the C # language class can only appear " =" and "- =" indicate an operator that adds to the cancel event response function.) Whether it is ASP.NET or a general Windows Forms programming, In C #, basically we encounter an event response method to illustrate the following form: Private Void Button1_Click (Object Sender, System.EventArgs E), an event response method access, return value type, parameter, and Does the type or even method name or the like must be fixed? The answer is: no! Under normal circumstances, there are two parameters in the response method of the event, one of which represents the object of the incident, the Sender, which is unpredictable, so we declare it as an object type, all objects apply. The second parameter represents the specific information of the incident, which may be different in various types of events, which is determined according to the instructions for event members. We know that the event is processed by delegate. It is assumed to be represented by representing events described in the following form: delegate int MyEventHandler (object sender, ToolBarButtonClickEventArgs e); then when it comes to the above event response function declaration, it must be declared as the following forms: private int MyTest (object sender, ToolBarButtonClickEventArgs e When adding an event response method to an object, you can use the following code to implement: control.Event = new myeventhandler (MyTest); generally, Java event processing is more direct, simple. And C # event processing due to reference agents, Make the program more flexible and more embody the looseness between the program. Stryon http://www.stryon.com.cn announces that Microsoft's .NET is realized on the Java development platform. Name is inet. The beta3 version of Inet in recent recently, including the three-level event handling mechanism for C # with Java.