MATLAB Object-Oriented Programming

(Joyce) #1

  • Saves the original property value

  • Sets the property to the specified value

  • If the specified value is greater than 10, the set method triggers an Overflow event

  • Passes the original property value, and other event data, in a
    SpecialEventDataClass object to the notify method.


classdef SimpleEventClass < handle
properties
Prop1 = 0
end
events
Overflow
end
methods
function set.Prop1(obj,value)
orgvalue = obj.Prop1;
obj.Prop1 = value;
if (obj.Prop1 > 10)
% Trigger the event using custom event data
notify(obj,'Overflow',SpecialEventDataClass(orgvalue));
end
end
end
end


Define Event Data


Event data is always contained in an event.EventData object. The
SpecialEventDataClass adds the original property value to the event data by
subclassing event.EventData:


classdef (ConstructOnLoad) SpecialEventDataClass < event.EventData
properties
OrgValue = 0
end
methods
function eventData = SpecialEventDataClass(value)
eventData.OrgValue = value;
end
end
end


Define Custom Event Data
Free download pdf