Lua class剖析

_setmetatableindex = function(t, index)
    if type(t) == "userdata" then
        local peer = tolua.getpeer(t)
        if not peer then
            peer = {}
            tolua.setpeer(t, peer)
        end
        _setmetatableindex(peer, index)
    else
        local mt = getmetatable(t)
        if not mt then mt = {} end
        if not mt.__index then
            mt.__index = index
            setmetatable(t, mt)
        elseif mt.__index ~= index then
            _setmetatableindex(mt, index)
        end
    end
end


function class(classname, ...) --参数一:所要创建的类名,参数二:可选参数,可以使function,也可以是table,userdata等
    local cls = {__cname = classname}

    local supers = {...}
    for _, super in ipairs(supers) do --遍历可选参数
        local superType = type(super)
          if superType == "function" then
            --如果是个function,那么就让cls的create方法指向他
            cls.__create = super
        elseif superType == "table" then --如果是个table
            if super[".isclass"] then--如果是个原生cocos类,比如cc.Sprite,不是自定义的
                 cls.__create = function() return super:create() end
            else
                -- 如果是个纯lua类,自己定义的那种,比如a={}
                cls.__supers = cls.__supers or {}
                cls.__supers[#cls.__supers + 1] = super--不断加到__supers的数组中
                if not cls.super then
                    -- 把第一个遍历到的table作为cls的超类
                    cls.super = super
                end
            end

        end
    end

    cls.__index = cls--不知道作用
    if not cls.__supers or #cls.__supers == 1 then --这个就是单继承,设置cls的元表的index为他的第一个超类
        setmetatable(cls, {__index = cls.super})
    else
        setmetatable(cls, {__index = function(_, key)--羡慕是多继承,index指向一个函数,到时候找元素的时候会遍历函数
            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
        -- 增加一个默认构造函数
        cls.ctor = function() end
    end
    cls.new = function(...) --新建方法,这个也是比较重要的方法
        local instance
        if cls.__create then
            --如果有create方法,那么就调用,正常情况下,自定义的cls是没有create方法的。
            --会不断的向上寻找元类的index,直到找到原生cocos类,比如sprite,然后调用sprite:create()
            --返回一个原生对象,通过调试代码,可以得出这些

            instance = cls.__create(...)
        else
            instance = {}--没有,说明根目录不是cocos类,而是普通类
        end
        --这个方法也比较关键,设置instance的元类index,谁调用new了,就把他设置为instance的元类index
        --具体可以看代码
        _setmetatableindex(instance, cls)
        instance.class = cls
        instance:ctor(...)--调用构造函数
        return instance
    end
    cls.create = function(_, ...)
        return cls.new(...)
    end

    return cls
end

        lua中没有类的概念,有的只是表(table),而类之间的继承也就是将父类的表连到了一起,派生类中没有找到的属性和方法就通过元表查找父类。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值