Bootstrap

Lua 类管理器

Lua 类管理器

-- ***** Class Manager 类管理*****‘

local ClassManager = {}

local this = ClassManager

function ClassManager.Class(className, ...)

    print('ClassManager::Class')
    --print(className)
    
    -- 构建类
    local cls = {__className = className}
    --print(cls)
    -- 父类集合
    local supers = {...}
    for _, super in pairs(supers) do
        -- 获取父类的类型
        local superType = type(super)
        --print(superType)
        assert(superType == nil or superType == 'table' or superType == 'function',
                string.format("class() - create class \"%s\" with invalid super class type \"%s\"",
                        className, superType))

        if superType == 'function' then
            assert(cls.__create == nil, string.format("class() - create class \"%s\" with more than one creating function",
                    className))
            cls.__create = super

        elseif superType == 'table' then
            if super['.isclass'] then
                assert(cls.__create == nil,
                        string.format("class() - create class \"%s\" with more than one creating function or native class",
                                className));

                cls.__create = function() super:create() end
            else
                -- 用来保存父类
                cls.__supers = cls.__supers or {}
                local dp = false
                for _, v in pairs(cls.__supers) do
                    if v.__className == super.__className then
                        dp = true
                        break
                    end
                end

                -- set first super pure lua class as class.super
                if not dp then
                    -- 将父类中所有的对象(变量或者函数)拷贝到子类中
                    cls.__supers[#cls.__supers + 1] = super
                    if not cls.super then
                        cls.super = super
                    end
                end

            end

        else
            error(string.format("class() - create class \"%s\" with invalid super type",
                    className), 0)
        end
    end



    cls.__index = cls

    if not cls.__supers or #cls.__supers == 1 then
        setmetatable(cls, {__index = cls.super})
    else
        -- 设置cls的元表为supers中的父类
        setmetatable(cls, {__index = function(_, key)
            local supers = cls.__supers
            for i=1, #supers do
                local super = supers[i]
                if super[key] then return super[key] end
            end
        end})
    end

    -- 添加默认构造函数
    if not cls.constructor then
        cls.constructor = function() end
    end

    -- new 方法构建类对象
    cls.new = function(...)
        -- 构建一个对象
        local instance
        if cls.__create then
            instance = cls.__create(...)
        else
            instance = {}
        end

        -- 设置对象的元表为当前类
        setmetatable(instance, cls)
        instance.class = cls
        instance:constructor(...)
        return instance

    end

    cls.create = function(_, ...)
        return cls.new(...)
    end

    -- 返回类
    return cls

end

local setmetatableindex = function(t, index)
    local mt = getmetatable(t)
    mt = mt or {}
    if not mt.__index then
        mt.__index = index
        setmetatable(t, mt)
    elseif mt.__index ~= index then
        setmetatableindex(mt, index)
    end
end

return ClassManager

使用

local MyObject = require('MyObject')
local ClassManager = require('ClassManager')
local obj3 = ClassManager.Class('obj3', MyObject)
--print(obj3)
obj3:myFunc()
obj3:myFunc2()
;