Cocos2dx-lua怎么實現(xiàn)面向?qū)ο?/h2>

類的實現(xiàn)

lua中其實是沒有類的,有的只是表(table),而類之間的繼承也就是將父類的表連到了一起,派生類中沒有找到的屬性和方法就通過元表查找父類

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));
            -- 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

    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 cls
end

上面是cocos官方對class的實現(xiàn)
ctor是他的構(gòu)造函數(shù)
可以用new或者create去實例化

類的運用

local dog = class("dog")

function dog:ctor ( ... )
    print("構(gòu)造函數(shù)")
end

function dog:eat( ... )
    print("狗吃屎")
end

local dog1 = dog.new()
dog1:eat()

這里聲明一個類dog,然后調(diào)用它的方法eat。

類的繼承

local dog = class("dog")

function dog:ctor ( ... )
    print("父類構(gòu)造函數(shù)")
end

function dog:eat( ... )
    print("狗吃屎")
end

local Huskie = class("Huskie",dog)

function Huskie:ctor( ... )
    Huskie.super.ctor(self)
    print("子類構(gòu)造函數(shù)")
end

local huskie1 = Huskie.create()
huskie1:eat()

Huskie(哈士奇)繼承dog,他也擁有dog的方法eat

注意繼承父類的方法需要先調(diào)用super的相應(yīng)的方法,第一個參數(shù)傳self。

繼承多個父類

--狗
local dog = class("dog")

function dog:ctor ( ... )
    print("dog構(gòu)造函數(shù)")
end

function dog:eat( ... )
    print("狗吃屎")
end

--金毛
local GoldenRetriever = class("GoldenRetriever")

function GoldenRetriever:ctor( ... )
    print("GoldenRetriever構(gòu)造函數(shù)")
end

function GoldenRetriever:cry( ... )
    print("金毛哭了")
end

--哈士奇
local Huskie = class("Huskie",dog,GoldenRetriever)

function Huskie:ctor( ... )
    Huskie.super.ctor(self)
    print("子類構(gòu)造函數(shù)")
end

local huskie1 = Huskie.create()
huskie1:eat()
huskie1:cry()

lua中 “...” 表示全部參數(shù)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容