Why are properties stored in "event.PropertyEvent.AffectedObject" instead of objects?
1 次查看(过去 30 天)
显示 更早的评论
The listener executes the callback function when MATLAB® triggers the property event. Define the callback function to have two specific arguments, which are passed to the function automatically when called by the listener:
- Event source — a meta.property object describing the object that is the source of the property event
- Event data — a event.PropertyEvent object containing information about the event
handle object
Object whose property is affected, specified as the object handle.
So,There should be an object stored in the ’-AffectedObject‘.But the result of code execution is not
Here is an example.make a breakingpoint in ClassB 22th line 'end'.
input in command window
>> obj_a=ClassA;
>> obj_b=ClassB(obj_a);
>> obj_a.y=4;
When program stop in ClassB 22th line .You check“evnt.AffectedObject”,you will find obj-a's property 'x' 'y' is stored here,not ’ obj-a‘
What is object ,include 'property' 'method' 'event'………………,not only the property.
So why are there only properties in ’evnt.AffectedObject‘, but not the full object?
classdef ClassA < handle
properties (SetObservable) %或(SetObservable=true)
x=1;
y=2;
end
events
kk
end
methods
function obj=ClassA()
x=1;
end
end
end
classdef ClassB < handle
methods
%构造函数,输入参数event_obj是产生事件的对象
function obj = ClassB(event_obj)
%为x的PreSet、为y的PostSet分别添加两个听众
if nargin > 0
addlistener(event_obj,'x','PreSet',@ClassB.eventsCBFunc);
addlistener(event_obj,'y','PostSet',@ClassB.eventsCBFunc);
end
end
end
methods (Static)
%两个听众的回调函数
function eventsCBFunc(src,evnt)
switch src.Name
case 'x'
fprintf(['Current event:' evnt.EventName]);
fprintf(1,' \n x is %s\n',num2str(evnt.AffectedObject.x))
case 'y'
fprintf(['Current event:' evnt.EventName]);
fprintf(1,' \n y is %s\n',num2str(evnt.AffectedObject.y))
end
end
end
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!