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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值