手游开发之lua的class函数详解
众所周知,lua没有类这个概念但其通过table实现了面向对象的“类”。在cocos2dx引擎下提供了class(className, ...)函数方法名,因为在脚本开发中这个接口基本都会用来创建一个lua对象,所以很重要。在之前的求职面试中,也有一些公司问到了这个实现的问题,如“lua中定义的class,使用了lua的哪些特性; class方法是如何实现模拟lua继承的呢?”等。
class有三种继承情况, 、第一种是继承一个方法, local LogoScene = class("LogoScene", function() return cc.Scene:create() end) 、第二种继承一个C语言原生类 local LogoScene = class("LogoScene", cc.Scene) 、继承一个lua类 local LogoScene = class("LogoScene", {})
下面就3.x逐行源码进行详细分析。
function class(classname, ...) local cls = {__cname = classname} --多继承 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)); cls.__create = super elseif superType == "table" then --若有.isclass这个值则说明是从c++转换过来的数据结构 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() return super:create() end else -- lua表 cls.__supers = cls.__supers or {} cls.__supers[#cls.__supers + ] = super if not cls.super then cls.super = super end end else error(string.format("class() - create class \"%s\" with invalid super type", classname), ) end end --lua元表:实现继承的重要元素,__index "查询"当子类搜索自己的函数未果,会按照__index的指引去搜索父类方法) cls.__index = cls then setmetatable(cls, {__index = cls.super}) --索引唯一父类 else setmetatable(cls, {__index = function(_, key) local supers = cls.__supers , #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 --C++层/lua方法 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 cls end
手游开发之lua的class函数详解的更多相关文章
- 手游开发之lua的table 元表的运用
元表在项目中的运用,其中就包括元方法这点.元方法是指__index和__newIndex,下面我总结下,更详细的例子讲解可以参考<lua程序设计 第2版>的第13章内容.长h短说,简言之有 ...
- cocos2dx+lua注册事件函数详解 事件
coocs2dx 版本 3.1.1 registerScriptTouchHandler 注册触屏事件 registerScriptTapHandler ...
- Python全栈开发之8、装饰器详解
一文让你彻底明白Python装饰器原理,从此面试工作再也不怕了.转载请注明出处http://www.cnblogs.com/Wxtrkbc/p/5486253.html 一.装饰器 装饰器可以使函数执 ...
- android开发之eclipse调试debug模式详解
之前我写了一个相关的帖子,但是今天看了一个还是写的比我详细,于是我拿过来和大家分享. 1.在程序中添加一个断点 如果所示:在Eclipse中添加了一个程序断点 在Eclipse中一共有三种添加断 ...
- cocos2dx+lua注册事件函数详解
coocs2dx 版本 3.1.1 registerScriptTouchHandler 注册触屏事件 registerScriptTapHandler 注册点击事件 registerScriptHa ...
- 【转】Android开发之Bitmap的内存优化详解
本文来源:转载自: http://mobile.51cto.com/abased-410796.htm 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitma ...
- android开发之wheel控件使用详解
出门在外生不起病呀,随便两盒药60多块钱.好吧,不废话了,今天我们来看看wheel控件的使用,这是GitHub上的一个开源控件,用起来十分方便,我们可以用它做许多事情,比如做一个自定义的datepic ...
- (转载)【cocos2dx 3.x Lua] 注册事件函数详解
出处: http://www.2cto.com/kf/201409/338235.html coocs2dx 版本 3.1.1 registerScriptTouchHandler 注册触屏事件 re ...
- Android开发之5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...
随机推荐
- win10 vs2015源码编译opencv、opencv_contrib、Tesseract
1.软件包准备 opencv源码包地址: 官网 github opencv_contrib源码包地址: github Tesseract源码包地址: ...
- css 优化
// 注: 以下内容大量借阅自<<Webkit技术内幕>>--朱永盛(14年出版的) , 很多内容可能早已更新 , 因此个人并不能确定论述是否正确.部分摘录内容有删减 , 目录 ...
- python之路——17
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.迭代器2.生成器3.内置函数 1.学习55个 2.带key的,max min filter map ...
- VC维的来龙去脉——转载
VC维的来龙去脉——转载自“火光摇曳” 在研究VC维的过程中,发现一篇写的很不错的VC维的来龙去脉的文章,以此转载进行学习. 原文链接,有兴趣的可以参考原文进行研究学习 目录: 说说历史 Hoeffd ...
- Spark RDD 操作
1. Spark RDD 创建操作 1.1 数据集合 parallelize 可以创建一个能够并行操作的RDD.其函数定义如下: ) scala> sc.defaultParallelism ...
- django Table doesn't exist
1146 django Table '' doesn't exist 一:出错原因 手动在数据库中drop了一张表,重新执行python manage.py migrate时出错,提示不存在这 ...
- mysql 聚集函数 count 使用详解(转载)
本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName)2.distinct 与 count 连用3.group by (多个字段) 与 ...
- mysql python 交互
一.安装 sudo apt-get install pymysql sudo pip3 install pymysql Connection对象 用于建立与数据库的连接 创建对象:调用connect( ...
- Unable to compile class for JSP 的可能原因
浏览器访问web jsp文件,导致500-Unable to compile class for JSP 的原因. 原因一:查看网上的方法,说很可能的原因是tomcat版本比JDK的版本要低导致的.所 ...
- 04 Javascript的数据类型 数组 函数
javascript:包含ECMAscript DOM BOM 描述了以下内容: 语法 类型 语句 关键字 保留字 运算符 对象 引入方式: <script> alert(123) < ...