编辑代码

function class(classname, super)
    local superType = type(super)
    local cls

    if superType ~= "function" and superType ~= "table" then
        superType = nil
        super = nil
    end

    if superType == "function" or (super and super.__ctype == 1) then
        -- inherited from native C++ Object
        cls = {}

        if superType == "table" then
            -- copy fields from super
            for k,v in pairs(super) do cls[k] = v end
            cls.__create = super.__create
            cls.super    = super
        else
            cls.__create = super
            cls.ctor = function() end
        end

        cls.__cname = classname
        cls.__ctype = 1

        function cls.new(...)
            local instance = cls.__create(...)
            -- copy fields from class to native object
            for k,v in pairs(cls) do instance[k] = v end
            instance.class = cls
            instance:ctor(...)
            return instance
        end

    else
        -- inherited from Lua Object
        if super then
            cls = {}
            setmetatable(cls, {__index = super})
            cls.super = super
        else
            cls = {ctor = function() end}
        end

        cls.__cname = classname
        cls.__ctype = 2 -- lua
        cls.__index = cls

        function cls.new(...)
            local instance = setmetatable({}, cls)
            instance.class = cls
            instance:ctor(...)
            return instance
        end
    end

    return cls
end

local ItemSystem = class("ItemSystem")
function ItemSystem:ctor()
    print("ItemSys ctor")
end
function ItemSystem:add_item(x)
    print("ItemSys add_item", x)
end
setmetatable(ItemSystem.add_item, {
    __call = function(this, ...)
        print('abc')
        return this(...)
    end
})
function ItemSystem:check_item()
    print("ItemSys check_item")
end
function ItemSystem:del_item()
    print("ItemSys del_item")
end

local M = {}
local model = {}
local t = {}
setmetatable(M, {
    __index = model
})
model.is_init = function()
    if model.game_server and model.account_id and model.user_id then
        return true
    end
    return false
end
model.game_server = 1
model.account_id = 2
model.set_system = function(sys_name, sys)
    sys.class.__call = function()
    if not sys.class.__call then
        sys.class.is_marked = true
        for k, v in pairs(sys.class) do
            if type(v) == 'function' and k ~= 'new' and k ~= 'ctor' then
                local f = v
                sys.class[k] = function(...)
                    if not model.is_init() then
                        print("error!!!!")
                    end
                    return f(...)
                end
            end
        end
    end
    model[sys_name..'_system'] = sys
end

M.set_system('item', ItemSystem:new())
M.set_system('item', ItemSystem:new())

M.item_system:add_item(1)
model.user_id = 3
M.item_system:add_item(1)