Lua多重继承
http://blog.csdn.net/ssihc0/article/details/7742421
代码收藏了,以后用的到 --多重继承 local function search(k,plist) for i=1,#plist do local v=plist[i][k] -- 取一个基类 if v then return v end end end function createClass(...) local c={}; local parents={...}; setmetatable(c,{__index=function(t,k) return search(k,parents) end } ); c.__index=c; function c:new(o) o=o or {}; setmetatable(o,c); return o; end return c; end Named={}; function Named:getname() return self.name; end function Named:setname(n) self.name=n; end Account={balance=0}; function Account:new(o) o= o or {} setmetatable(o,self); self.__index=self; return o; end function Account.deposit(self,v) self.balance=self.balance+v; end function Account:withdraw(v) if (v) > self.balance then error "insufficient funds"; end self.balance=self.balance-v; end NamedAccount=createClass(Account,Named); user=NamedAccount:new{name="user admin"}; print (user:getname(),user.balance);
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 local class2 = {} function class2:new() local obj = {} setmetatable(obj, {__index= class2}) return obj end function class2:print2() print("class2: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 class3 = {} function class3:new() local obj = {} local base = {class1, class2} --t为调用的对象,k为方法名 setmetatable(class3, {__index= function(t,k) return super(k, base) end}) setmetatable(obj, {__index = class3}) return obj end