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,直接使用。
阅读全文
0 0
- lua继承小记
- lua继承
- lua继承
- lua继承
- Lua API 小记
- lua基础小记
- Lua API 小记1
- Lua学习小记
- Lua API 小记1
- Lua正则表达式小记
- JavaScript原型继承小记
- 继承,多态小记
- 多继承小记
- lua API 小记3(lua虚拟机初始化)
- lua 类、继承、多重继承
- lua 类与继承
- lua 之多重继承
- lua实现继承
- ROS Kinetic: msg & srv
- iOS逆向工程---OS工具(Theos)
- 整合SpringMVC+Spring+Mybatis+mysql的maven配置文件pom.xml
- logback logback.xml常用配置详解(一)<configuration> and <logger>
- linux几种关机命令总结
- lua继承小记
- 设计模式---组合模式
- WWDC2017全球开发者大会汇总
- React Native 嵌入原生项目混合开发-多个RN Activity
- hadoo分布式文件操作HDFS
- 双击jar文件运行程序
- 十图详解TensorFlow数据读取机制(附代码)
- Glide图片框架使用详细介绍(五)之Glide-源码详解
- ubuntu14.04配置caffe