Lua 类管理器

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

使用

lua 复制代码
local MyObject = require('MyObject')
local ClassManager = require('ClassManager')
local obj3 = ClassManager.Class('obj3', MyObject)
--print(obj3)
obj3:myFunc()
obj3:myFunc2()
相关推荐
froginwe1110 分钟前
R 基础运算
开发语言
醉城夜风~10 分钟前
[数据结构]堆详解
开发语言·数据结构
17´37 分钟前
Qt从入门到入土(八) -打包Qt程序
开发语言·c++·qt
AI+程序员在路上38 分钟前
QT显示网页控件QAxWidget、QWebEngineView及区别
开发语言·qt
南玖yy44 分钟前
C语言柔性数组深度解析:动态内存管理的艺术
c语言·开发语言·柔性数组
2301_764441331 小时前
python实现的生态模拟系统
开发语言·python·pygame
无世世1 小时前
【Java从入门到起飞】面向对象编程(高级)
java·开发语言
q567315231 小时前
使用CPR库编写的爬虫程序
开发语言·爬虫·golang·音视频
星之卡比*1 小时前
前端0基础---day18Math - Date - 定时器 (javascript)
开发语言·前端·javascript
星辰tsy1 小时前
JS内存泄漏问题
开发语言·javascript