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()
相关推荐
凤城老人15 小时前
C++使用拉玛努金公式计算π的值
开发语言·c++·算法
HAH-HAH17 小时前
【Python 入门】(2)Python 语言基础(变量)
开发语言·python·学习·青少年编程·个人开发·变量·python 语法
递归不收敛18 小时前
一、Java 基础入门:从 0 到 1 认识 Java(详细笔记)
java·开发语言·笔记
zhangfeng113319 小时前
win7 R 4.4.0和RStudio1.25的版本兼容性以及系统区域设置有关 导致Plots绘图面板被禁用,但是单独页面显示
开发语言·人工智能·r语言·生物信息
子午20 小时前
Python的uv包管理工具使用
开发语言·python·uv
HMBBLOVEPDX21 小时前
C++(静态函数)
开发语言·c++
dpxiaolong1 天前
RK3588 Android12默认移除导航栏
开发语言·python
Pocker_Spades_A1 天前
Python快速入门专业版(二十九):函数返回值:多返回值、None与函数嵌套调用
服务器·开发语言·python
良木林1 天前
浅谈原型。
开发语言·javascript·原型模式
烈风1 天前
004 Rust控制台打印输出
开发语言·后端·rust