众所周知,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函数详解的更多相关文章

  1. 手游开发之lua的table 元表的运用

    元表在项目中的运用,其中就包括元方法这点.元方法是指__index和__newIndex,下面我总结下,更详细的例子讲解可以参考<lua程序设计 第2版>的第13章内容.长h短说,简言之有 ...

  2. cocos2dx+lua注册事件函数详解 事件

    coocs2dx 版本 3.1.1 registerScriptTouchHandler             注册触屏事件 registerScriptTapHandler             ...

  3. Python全栈开发之8、装饰器详解

    一文让你彻底明白Python装饰器原理,从此面试工作再也不怕了.转载请注明出处http://www.cnblogs.com/Wxtrkbc/p/5486253.html 一.装饰器 装饰器可以使函数执 ...

  4. android开发之eclipse调试debug模式详解

     之前我写了一个相关的帖子,但是今天看了一个还是写的比我详细,于是我拿过来和大家分享. 1.在程序中添加一个断点 如果所示:在Eclipse中添加了一个程序断点 在Eclipse中一共有三种添加断 ...

  5. cocos2dx+lua注册事件函数详解

    coocs2dx 版本 3.1.1 registerScriptTouchHandler 注册触屏事件 registerScriptTapHandler 注册点击事件 registerScriptHa ...

  6. 【转】Android开发之Bitmap的内存优化详解

    本文来源:转载自: http://mobile.51cto.com/abased-410796.htm 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitma ...

  7. android开发之wheel控件使用详解

    出门在外生不起病呀,随便两盒药60多块钱.好吧,不废话了,今天我们来看看wheel控件的使用,这是GitHub上的一个开源控件,用起来十分方便,我们可以用它做许多事情,比如做一个自定义的datepic ...

  8. (转载)【cocos2dx 3.x Lua] 注册事件函数详解

    出处: http://www.2cto.com/kf/201409/338235.html coocs2dx 版本 3.1.1 registerScriptTouchHandler 注册触屏事件 re ...

  9. Android开发之5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

随机推荐

  1. Linux下自己实现getopt功能

    实现思路: 通过 pid_t pid = getpid() 来获取当前进程id,然后 sprintf(fname, "/proc/%d/cmdline", pid); 读取fnam ...

  2. ubuntu16.04下latex环境搭建

    背景: 最近需要使用latex做一下简历~~~ 工具: sublime text3 (plugin: latextools) + texlive-full 配置: Preferences -> ...

  3. Python中的取模运算

    C++中的取模运算符%只能对整数使用(如果要对浮点数使用需要fmod),Python则不同,对整数或浮点数均有效. 在这里再介绍一下取模的定义:假设a,b两个数,那么a mod b = a - n*b ...

  4. Neject 在MVC框架中使用

    Neject 开始是用3.3.0.0,不能自动生成NinjectWebCommon文件,测试了很久发现,是版本的问题 ,后来用Nuget卸载后,重新下了Ninject,Ninject.Web.Comm ...

  5. 实时监听input输入内容的N种方法

    现在有一个需求,需要我们实时监听input输入框中的内容,从而带来更好的用户体验,而不是等我们全部输入完毕才告诉我们格式不对首先我们创建一个input输入框 <form name='loginF ...

  6. bootstrapvalidator的简单校验【必填校验、长度校验、是否存在校验(remote)】

    需求:输入框的"供应商编码"不能为空而且不能与数据库重复,供应商名称不能为空. 解决: 1.input如下 <input id="ssupplierNo" ...

  7. 2008R2 部署 aspnetcore repair failed 函数不正确

    vc_redist.x64

  8. Linux下查看tomcat版本

    进入到tomcat的bin目录下,再执行./version.sh tomcat版本:7.0

  9. ---Ubuntu 16.04 server 不能关机问题解决

    https://serverfault.com/questions/712928/systemctl-commands-timeout-when-ran-as-root Failed to start ...

  10. Linux连接虚拟机及操作指令

    Linux的安装(虚拟机环境)与基础配置   一.背景 本文介绍如何安装虚拟机VMware以及如果在虚拟机上安装Linux系统以及Linux安装完毕之后的基础配置 需要准备的东西有VMware以及Li ...