编辑代码

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
function ItemSystem:check_item()
    print("ItemSys check_item")
end
function ItemSystem:del_item()
    print("ItemSys del_item")
end

local User = class("User")

function User:ctor(game_server, account_id)
    self.game_server = game_server
    self.account_id = account_id
end

function User:is_init()
    if not self.game_server then
        print("model is not init, lack of game server")
        return false
    end
    if not self.account_id then
        print("model is not init, lack of account id")
        return false
    end
    if not self.user_id then
        print("model is not init, lack of user id")
        return false
    end
    return true
end

function User:set_system(name, instance)
    self[name.."_system"] = instance
    for instance_k, instance_v in pairs(instance.class) do
        if type(instance_v) == 'function' and instance_k ~= 'new' and instance_k ~= 'ctor' then
            instance[instance_k] = function(...)
                if not self:is_init() then
                    return
                end
                instance_v(instance, ...)
            end
        end
    end
end

local user = User.new(1, 2)
user:set_system("item", ItemSystem.new(user))

user.item_system:add_item(10)

-- 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)