Package:event
Data for property events
Theevent.PropertyEvent
class defines the event data objects passed to listeners of these predefined property events:
PreGet
PostGet
PreSet
PostSet
Predefined property events enable listeners to respond to changes made to property values. For more information, seeListen for Changes to Property Values.
Theevent.PropertyEvent
class is a sealed subclass ofevent.EventData
(that is, you cannot subclassevent.PropertyEvent
). The class constructor is private. MATLAB®creates anevent.PropertyEvent
object to pass to listeners of property events.
Theevent.PropertyEvent
class is ahandle
class.
Sealed |
true
|
ConstructOnLoad |
true
|
HandleCompatible |
true
|
RestrictsSubclassing |
true
|
For information on class attributes, seeClass Attributes.
Define thepropEventClass
类的PropOne
GetObservable
andSetObservable
attributes enabling observation of property events. The class constructor adds listeners for these events.
classdefpropEventClass < handle%一堂课s to observe property eventsproperties(GetObservable,SetObservable) PropOnestring="default"endmethodsfunctionobj = propEventClass addlistener(obj,'PropOne','PreGet',@propEventHandler); addlistener(obj,'PropOne','PostSet',@propEventHandler);endendend
ThepropEventHandler
function serves as the callback for thePreGet
andPostSet
事件。
Theevent.PropertyEvent
object所以urce
property contains themeta.property
object forPropOne
. Access themeta.property
Name
property to get the name of the property on which the event is triggered. Switch on the property name when the callback handles multiple properties.
Theevent.PropertyEvent
objectEventName
property contains the name of the event. To handle multiple property events from the callback, switch on the event name.
functionpropEventHandler(~,eventData)switcheventData.Source.Name% Get property namecase'Prop1'switcheventData.EventName% Get the event namecase'PreGet'fprintf('%s\n','***PreGet triggered***')case'PostSet'fprintf('%s\n','***PostSet triggered***') disp(eventData.AffectedObject.(eventData.Source.Name));endendend
Referencing thePropOne
property value results in a response from thepropEventHandler
to thePreGet
event.
obj = propEventClass; obj.PropOne
***PreGet triggered*** ans = "default"
Assigning to thePropOne
property results in a response from thepropEventHandler
to thePostSet
event.
Because the callback gets the property value to display the new value after thePostSet
event, thePreGet
event is triggered. Also, because the assignment statement is not terminated by a semicolon, MATLAB gets the property value to display the object in the command window, which triggers thePreGet
事件再次。
obj.PropOne ="New string"
***PostSet triggered*** ***PreGet triggered*** New string obj = ***PreGet triggered*** propEventClass with properties: PropOne: "New string"