lua中table的单继承与多继承
Table单继承(元表)
account = {}
function account:new( o )
o = o or {}
setmetatable(o,self)
self.__index = self
return o
-- body
end
function account:show( v )
print(self.bb…v)
– body
end
a = account:new({bb = 'aa'})
a:show('bb') – aabb
Table多继承(不常用)
local function search(k,plist )
– body
for i=1,#plist do
local v = plist[i][k]
if v then
return v
end
end
end
function createClass( … )
– body
local c = {}
local parent = {…}
setmetatable(c,{__index = function ( t,k )
– body
return search(k,parent)
end})
c.__index = c
function c:new(o )
-- body
o = o or {}
setmetatable(o,c)
return o
end
end