The Event ModelEvents represent changes or actions that occur within class instances. For example,
Basically, any activity that you can detect programmatically can generate an event and communicate information to other objects. MATLAB? classes define a process that communicates the occurrence of events to other objects that need to respond to the events. The event model works this way:
The following diagram illustrates the event model. Default Event DataEvents provide information to listener callbacks by passing an event data argument to the callback function. By default, MATLAB passes anevent.EventData object to the listener callback. This object has two properties:
MATLAB passes the source object to the listener callback in the required event data argument. This enables you to access any of the object's public properties from within your listener callback function. Customizing Event DataYou can create a subclass of the event.EventData class to provide additional information to listener callback functions. The subclass would define properties to contain the additional data and provide a method to construct the derived event data object so it can be passed to the notify method. Defining Event-Specific Data provides an example showing how to customize this data. Events Only in Handle ClassesYou can define events only in handle classes. This restriction exists because a value class is visible only in a single MATLAB workspace so no callback or listener can have access to the object that triggered the event. The callback could have access to a copy of the object. However, accessing a copy is not generally useful because the callback cannot access the current state of the object that triggered the event or effect any changes in that object. Comparing Handle and Value Classes provides general information on handle classes. Events and Listeners — Syntax and Techniques shows the syntax for defining a handle class and events. Property-Set and Query EventsThere are four predefined events related to properties:
These events are predefined and do not need to be listed in the class events block. When a property event occurs, the callback is passed an event.PropertyEvent object. This object has three properties:
You can define your own property-change event data by subclassing the event.EventData class. Note that the event.PropertyEventclass is a sealed subclass of event.EventData. See Listen for Changes to Property Values for a description of the process for creating property listeners. See The PostSet Event Listener for an example. See Property Access Methods for information on methods that control access to property values. ListenersListeners encapsulate the response to an event. Listener objects belong to the event.listener class, which is a handle class that defines the following properties:
Ways to Create Listeners provides more specific information. |
|
来自: windycityboy > 《Event&Listen》