lua实现多继承

http://my.oschina.net/u/156466/blog/401576
local class1 = {} function class1:new() local obj = {} setmetatable(obj, {__index= class1}) return obj end function class1:print1() print("class1:print()") end function class1:print2() print("class1:print()") end local class2 = {} function class2:new() local obj = {} setmetatable(obj, {__index= class2}) return obj end function class2:print2() print("class2:print()") end local class3 = {} function class3:new() local obj = {} setmetatable(obj, {__index= class3}) return obj end function class3:print2() print("class3:print()") end function class3:print3() print("class3:print()") end function super(k, list) for i,v in ipairs(list) do local ret = v[k] if ret then return ret end end end local class = {} function class:new(...) local obj = {} -- local base = {class1, class2} local base = {...}; --t为调用的对象,k为方法名 setmetatable(class, {__index= function(t,k) return super(k, base) end}) setmetatable(obj, {__index = class}) return obj end local c = class:new(class2,class1,class3); -- c:print1(); c:print2(); -- c:print3();

 

posted on 2016-02-04 11:48  602147629  阅读(989)  评论(0编辑  收藏  举报