• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

lua如何构造类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 1 function class(super, autoConstructSuper)
 2     local classType = {};
 3     classType.autoConstructSuper = autoConstructSuper or (autoConstructSuper == nil);
 4     
 5     if super then
 6         classType.super = super;
 7         local mt = getmetatable(super);
 8         setmetatable(classType, { __index = super; __newindex = mt and mt.__newindex;});
 9     else
10         classType.setDelegate = function(self,delegate)
11             self.m_delegate = delegate;
12         end
13     end
14 
15     return classType;
16 end
17 
18 
19 function new(classType, ...)
20     local obj = {};
21     local mt = getmetatable(classType);
22     setmetatable(obj, { __index = classType; __newindex = mt and mt.__newindex;});
23     do
24         local create;
25         create =
26             function(c, ...)
27                 if c.super and c.autoConstructSuper then
28                     create(c.super, ...);
29                 end
30                 if rawget(c,"ctor") then
31                     obj.currentSuper = c.super;
32                     c.ctor(obj, ...);
33                 end
34             end
35 
36         create(classType, ...);
37     end
38     obj.currentSuper = nil;
39     return obj;
40 end
41 
42 
43 function delete(obj)
44     do
45         local destory =
46             function(c)
47                 while c do
48                     if rawget(c,"dtor") then
49                         c.dtor(obj);
50                     end
51               
52                     c = getmetatable(c);
53                     c = c and c.__index;                   
54                 end
55             end
56         destory(obj);
57     end
58 end

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能发布时间:2022-07-22
下一篇:
lua正则匹配开头和结尾的空字符发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap