手游开发之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 ...
随机推荐
- DB2锁与隔离级别
数据库管理器支持三种一般类别的锁定: 共享(S) 挂起 S 锁定之后,并发应用程序进程只能对数据执行只读操作. 更新(U) 挂起 U 锁定之后,如果并发应用程序进程未声明它们要更新行,那么它们只能对数 ...
- [UE4]抛物线指示器
一.抛物线的有以下4中函数 二.本例使用Predict Projectile Path By TraceChannel方法. 抛物线三要素:发射位置.发射角度.发射速度. 1.Start Pos:起始 ...
- springmvc功能以及源码实现分析
每当我学习一个新技术时,我总是会问自己这个技术有哪些功能,能帮我解决哪些问题. 但是当我在网上进行搜索时,答案并不让我满意. 在使用springmvc很久以后,我将在这篇文章里对springmvc功能 ...
- Centos 6.4 安装Python 2.7 python-pip
1. 准备工作 下载源码包 wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 查看是否安装make工具 ~#rpm -qa|gr ...
- C#将unix时间戳转换成.net的DateTime类型的代码
下面的内容是关于C#将unix时间戳转换成.net的DateTime类型的内容. DateTime epoch = new DateTime(1970,1,1,0,0,0,0, DateTimeKin ...
- Python 编码的一些问题
>>> ord('中') 20013 >>> chr(65) 'A' >>> chr(20013) '中' - Python文件默认是UTF-8编 ...
- python 日期换算星期 蔡勒公式
#!/usr/bin/env python # encoding: utf-8 #coding=utf-8 date_star={ ':'星期一', ':'星期二', ':'星期三', ':'星期四' ...
- CentOS7 部署zabbix4.2
zabbix我就不介绍了吧,但是可能又有些小白,我还是介绍一下吧,嘿嘿! 一:什么是zabbix及优缺点(对比cacti和nagios) Zabbix能监视各种网络参数,保证服务器系统的安全运营:并提 ...
- jQuery 正则数字验证、’Money验证、电话号码验证、身份证验证、邮箱验证、网址验证、敏感字符验证
jQuery只能输入数字 第一种写法: <input onkeyup="value=value.replace(/[^\d]/g,'')" /> 第二种写法: < ...
- Javascript面向对象编程(一)
什么是面向对象? 它是一种新的编程模式.以往的面向过程是将功能都写在一起,逐行实现.面向对象与其不同,它只关心对象提供的功能,而不在乎细节. 面向对象的特点? 抽象:抓住问题的核心,贯穿始终: 封装: ...