码迷,mamicode.com
首页 > 其他好文 > 详细

lua如何构造类

时间:2014-09-06 18:32:03      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   div   cti   sp   log   

 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

 

lua如何构造类

标签:des   style   blog   color   io   div   cti   sp   log   

原文地址:http://www.cnblogs.com/jadeboy/p/3959606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!