美文网首页
Cocos2dx-lua怎么实现面向对象

Cocos2dx-lua怎么实现面向对象

作者: 游戏创作者 | 来源:发表于2020-10-10 10:31 被阅读0次

类的实现

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的实现
ctor是他的构造函数
可以用new或者create去实例化

类的运用

local dog = class("dog")

function dog:ctor ( ... )
    print("构造函数")
end

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

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

这里声明一个类dog,然后调用它的方法eat。

类的继承

local dog = class("dog")

function dog:ctor ( ... )
    print("父类构造函数")
end

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

local Huskie = class("Huskie",dog)

function Huskie:ctor( ... )
    Huskie.super.ctor(self)
    print("子类构造函数")
end

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

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

注意继承父类的方法需要先调用super的相应的方法,第一个参数传self。

继承多个父类

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

function dog:ctor ( ... )
    print("dog构造函数")
end

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

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

function GoldenRetriever:ctor( ... )
    print("GoldenRetriever构造函数")
end

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

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

function Huskie:ctor( ... )
    Huskie.super.ctor(self)
    print("子类构造函数")
end

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

lua中 “...” 表示全部参数

相关文章

  • Cocos2dx-lua怎么实现面向对象

    类的实现 lua中其实是没有类的,有的只是表(table),而类之间的继承也就是将父类的表连到了一起,派生类中没有...

  • 面向对象基础

    面向对象编程包括: 面向对象的分析(OOA) 面向对象的设计(OOD) 面向对象的编程实现(OOP) 面向对象思想...

  • 《软件工程》笔记7

    面向对象的实现 面向对象的程序设计最好还是选用面向对象的编程语言。 良好的程序设计风格对于面向对象实现来说格外重要...

  • 面向对象基础

    一、面向对象概述 1、面向对象编程包括: OOA:面向对象分析OOD:面向对象的设计OOP:面向对象的编程实现 2...

  • java基础回顾(2)

    面向对象:是基于面向过程的一种思想。 面向过程:以函数为基础,关注实现过程。 面向对象:以对象为基础,关注实现结果...

  • js中利用面向过程和面向对象实现圆点碰壁反弹

    使用面向过程实现碰壁反弹 使用面向对象实现碰壁反弹

  • 类与对象(1) - Class、Object

    面向对象编程思想 Java开发是利用 面向对象 的思想实现 面向过程 的开发。 面向对象编程(OOP:Object...

  • java面向对象

    面向对象和面向过程的思想 面向过程的程序设计方式:遇到一件事,思考“我该怎么做”,然后一步步实现过程; 面向对象的...

  • Python基础入门 - 面向对象

    1. 初识面向对象 1.1 介绍 步骤介绍面向对象的概述面向对象的实现面向对象的应用内存管理进程、线程、协程 概要...

  • java 面试笔试题(一)

    一,什么是面向对象 答: 面向对象是一种编程思想,在使用对象的过程中不需要管对象内部是怎么实现的,只需要知道可以...

网友评论

      本文标题:Cocos2dx-lua怎么实现面向对象

      本文链接:https://www.haomeiwen.com/subject/yyedpktx.html