Why does the callback function create an 'meta.property 'object
Afficher commentaires plus anciens
When you wanna instantiate an object. First you need creat a class or you already have a class. Then you use a class to instantiate an object.
Or you already object, then you copy a object .
But look this code. I make a breakingpoint in 15th line of ClassB 'switch src.Name',then excute:
>> obj_a=ClassA;
>> obj_b=ClassB(obj_a);
>> obj_a.x=3;
When the program runs to 15th line of ClassB.m . I found 'src' is a 'meta.property 'object! Why does the callback function create an 'meta.property 'object? How does the callback function create an 'meta.property 'object?
I check the addlistener ,this function allow to input 'meta.property 'object for 'PropertyName' ,but it dose not creat a 'meta.property 'object.
Obviously 'x' as the input arguments of the addlistener function(7th line of ClassB.m) is the character, not the 'meta.property' object.
ClassA.m
classdef ClassA < handle
properties (SetObservable) %或(SetObservable=true)
x=1;
y=2;
end
end
ClassB.m
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
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Class Introspection and Metadata dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!