MagicListener

版本 1.0.0.0 (2.7 KB) 作者: Benoit Charles
MagicListener object
450.0 次下载
更新时间 2012/1/16

查看许可证

MagicListener constructor creates a listener just like the addlistener function. The difference is the constructor accepts one more input, one object or a cell of objects, and the life cycle of the returned object will be linked also on these objects.

Exemple:

Without MagicListener:

[Class1.m]
classdef Class1 < handle
events
myEvent
end
end

[Class2.m]
classdef Class2 < handle
methods
function this = Class2( obj1 )
addlistener( obj1, 'myEvent', @(H,E) this.callback() );
end
function callback( this )
disp( 'CALLBACK!' );
end
end
end

>> obj1 = Class1();
>> obj2 = Class2( obj1 );
>> notify( obj1, 'myEvent' );
CALLBACK!
>> delete( obj2 );
>> notify( obj1, 'myEvent' );
Warning: Error occurred while executing callback:
Invalid or deleted object.

With MagicListener:

[Class1.m]
classdef Class1 < handle
events
myEvent
end
end

[Class2.m]
classdef Class2 < handle
methods
function this = Class2( obj1 )
MagicListener( obj1, 'myEvent', @(H,E) this.callback(), this );
end
function callback( this )
disp( 'CALLBACK!' );
end
end
end

>> obj1 = Class1();
>> obj2 = Class2( obj1 );
>> notify( obj1, 'myEvent' );
CALLBACK!
>> delete( obj2 );
>> notify( obj1, 'myEvent' );

引用格式

Benoit Charles (2024). MagicListener (https://www.mathworks.com/matlabcentral/fileexchange/34606-magiclistener), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2008b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Handle Classes 的更多信息
标签 添加标签
致谢

启发作品: Graphical Wrappers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0