LUA多重継承の共有


LObject = {name = "aaa"}

function LObject:new(o)
    self.__index = self
    o = o or {}
    setmetatable(o,self)
    return o
end

function LObject:test()
    print(self.name)
end

LHuman = {age = 0}

function LHuman:new(o)
    self.__index = self
    o = o or {}
    setmetatable(o,self)
    return o
end

function LHuman:ptest()
    print(self.name.."   "..self.age)
end

function Class(...)
    local function search(k, plist)
        for i = 1,#plist do
            local v = plist[i][k]
            if v then return v end
        end
    end
    local c = {}
    local parents = {...}
    setmetatable(c, {__index = function(t, k) return search(k, parents) end})
    function c:new(o)
        o = o or {}
        setmetatable(o, c)
        c.__index = c
        return o
    end
    return c
end

LWoman = Class(LObject,LHuman)

yangxiumiao = LWoman:new({name="Yang Xiu Miao",age=27})

yangxiumiao:ptest()