lua继承小记

来源:互联网 发布:驱动器加密软件 编辑:程序博客网 时间:2024/06/07 12:53
--在游戏界面长时间不操作收到的服务器让我托管function RoomScene:onRcvTuoguan(msgTbl)    if not msgTbl.m_nPos or not msgTbl.m_nType then        return    end    if (msgTbl.m_nPos + 1)==self:getRoom():getMainSeatIdx() then        if msgTbl.m_nType == 1 then  --托管            local runningScene = cc.Director:getInstance():getRunningScene()            if runningScene.__cname ~= "RoomScene" then   --当前处在其它界面                self.isInOtherScene = true            else                self:getDecisionNode():removeAllChildren()                self:getDecisionNode():setVisible(false)                Tools.addShieldLayer()            end        elseif msgTbl.m_nType == 0 then  --取消托管            self:getDecisionNode():setVisible(true)            Tools.removeShieldLayer()        end    end  end
上面这个是基类。其中getDecisionNode 这个方法其实是子类的RoomScene中的方法。但是却在父类中使用了。

function class(classname, ...)    local cls = {__cname = classname}    --这样就是lua的多继承了    local supers = {...}    for _, super in ipairs(supers) do        local superType = type(super)        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));            -- if super is function, set it to __create            cls.__create = super        elseif superType == "table" then            if super[".isclass"] then                -- super is native class                assert(cls.__create == nil,                    string.format("class() - create class \"%s\" with more than one creating function or native class",                        classname));                cls.__create = function() return super:create() end            else                -- super is pure lua class                cls.__supers = cls.__supers or {}                cls.__supers[#cls.__supers + 1] = super                if not cls.super then                    -- set first super pure lua class as class.super                    cls.super = super                end            end        else            error(string.format("class() - create class \"%s\" with invalid super type",                        classname), 0)        end    end    --这里很关键,__index = cls表明,先从子类中查找,查找不到,就找父类的。    cls.__index = cls    if not cls.__supers or #cls.__supers == 1 then        setmetatable(cls, {__index = cls.super})    else        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.ctor then        -- add default constructor        cls.ctor = function() end    end    cls.new = function(...)        local instance        if cls.__create then            instance = cls.__create(...)        else            instance = {}        end        setmetatableindex(instance, cls)        instance.class = cls        instance:ctor(...)        return instance    end    cls.create = function(_, ...)        return cls.new(...)    end    return clsend

因此还是那个面使用getDecisionNode的过程是:先找子类的,直接找到。然后就ok,直接使用。