惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
罗磊的独立博客
S
Secure Thoughts
Schneier on Security
Schneier on Security
博客园 - Franky
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
S
Security Affairs
SecWiki News
SecWiki News
博客园 - 聂微东
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
H
Heimdal Security Blog
S
Security @ Cisco Blogs
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cloudbric
Cloudbric
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Visual Studio Blog
P
Proofpoint News Feed
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
W
WeLiveSecurity
Last Week in AI
Last Week in AI
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
S
Securelist
GbyAI
GbyAI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
CERT Recently Published Vulnerability Notes
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
B
Blog RSS Feed
P
Palo Alto Networks Blog
H
Hacker News: Front Page
D
Docker
雷峰网
雷峰网
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog

博客园 - simonw

Lua5.1中可变参数...对性能的影响 - simonw - 博客园 IIS7权限简述 MoinMoin Wiki 1.7优化与维护经验 - simonw WoW安全模板技术文档翻译 - SecureStateHeader 雕虫小技之MoinMoin Wiki与Discuz论坛的单点登陆整合 雕虫小技之为Discuz论坛添加Wiki语法 - simonw - 博客园 MoinMoin Wiki 在Linux Apache下的安装 Wiki你长的为何与众不同--对Wiki独创写作语法原因的思考 MoinMoin Wiki 在IIS下的安装与升级 Metatable In Lua 浅尝辄止 MoinMoin Wiki 权限使用指南 MoinMoin Wiki 引擎概览 Wiki服务商简介 N-Layer, COP, SOA In WOW Addon Develop Ioc容器应用浅析 WOW插件:ShortStory 1.3 发布(2007.7.5) Castle ActiveRecord 泛型应用 WOW插件:ShortRobot 1.21 发布(2006.10.3) WOW插件:ShortUnitFrame 2.1 发布(2007.7.17)
Lua中实现类似C#的事件机制
simonw · 2006-12-20 · via 博客园 - simonw

Lua的语法非常灵活, 使用他的metatable及metamethod可以模拟出很多语言的特性.

C#中我们这样使用事件:

xxx.Click += new System.EventHandler(xxx_Click);private void xxx_Click(object sender, EventArgs e)
{
    
/**/
}

在Lua中要达到同样的效果, 并且支持事件多播机制, 其关键在于重写metamethod __call, 从而使得不光function才能被调用, table也能够被调用.

主要思想就是, 通过一个table来保存注册事件的若干响应函数, 然后拿table当function一样来调用, 重写__call后, 实现调用table时遍历执行table中的注册方法.

需要在lua5.0 或 lua.net上执行, lua 5.1略有改动.

--test.lua
do--事件原型对象, 所有事件由此原型生成
Event = {}function Event:New()
    local 
event = {}
    setmetatable(
event, self)
    
--覆盖__index逻辑
    self.__index 
= self
    
--覆盖__call逻辑
    self.__call 
= self.Call
    
return event
end--事件注册, 通过此方法将响应方法注册到事件上. 
--@source:响应方法的所属对象
--@func:响应方法
function Event:Add(source, func)
    table.insert(self, {source, func})    
end--内部方法, 重写了默认__call逻辑, 当event被触发调用时, 循环执行event中注册的响应方法
--@table:对象产生调用时将本身传入
--@:调用参数
function Event.Call(table, )    
    
for _, item in ipairs(table) do
        
--item[1]就是source, item[2]就是func响应方法
        
--lua 5.1中无需使用unpack(arg), 直接使用即可
        item[
2](item[1], unpack(arg))
    
end
end------------------以下为测试用例-------------------------创建一个window对象, 注册按钮的点击事件
Window 
= {
    Name 
= "Simonw's Window",    
}
function Window:Init()
    
--注册事件, self即Window, 对象来源.
    Button.ClickEvent:Add(self, self.Button_OnClick)    
end--响应事件方法, sender即是传来的Button对象
function Window:Button_OnClick(sender)    
    
print(sender.Name.." Click On "..self.Name)
end--创建一个button对象, 拥有ClickEvent这样的事件
Button 
= {
    Name 
= "A Button",
    
--创建事件
    ClickEvent 
= Event:New(),
}
--执行点击按钮的动作
function Button:Click()
    
print('Click begin')
    --触发事件, self即sender参数
    self.ClickEvent(self)
    
print('Click end')
end--从这里执行
Window:Init()
Button:Click()
--[[
执行结果:
> dofile 'test.lua'
Click begin
A Button Click 
On Simonw's Window
Click end
]]
end