当前位置: 首页 > news >正文

Lua之面向对象的实现

Lua本身是不支持面向对象的,但是可以利用table和元表、元方法进行面向对象的模拟!
以下是Cocos2d-lua中面向对象的实现!

class

function class(classname, ...)
    local cls = {__cname = classname}

    local supers = {...}
    for _, super in ipairs(supers) do
        local superType = type(super)
        -- 基类的类型只能是table或者function(若为nil,则无基类),其他类型是无效的
        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  -- 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)  -- setmetatableindex见附录
        instance.class = cls
        instance:ctor(...)
        return instance
    end
    cls.create = function(_, ...)
        return cls.new(...)
    end

    return cls
end

Test Demo

基类:BaseClass.lua

local BaseClass = class("BaseClass")
 
function BaseClass:ctor()
    print("BaseClass:ctor")
end
 
function BaseClass:printFunc()
    print("this is BaseClass`s printFunc")
end

function BaseClass:testFunc()
    print("this is BaseClass`s testFunc")
end
 
return BaseClass

派生类:DerivativeClass.lua

local baseClass = require("BaseClass"):create()

local DerivativeClass = class("DerivativeClass", baseClass)
 
function DerivativeClass:ctor()
    print("DerivativeClass:ctor")
end
 
function DerivativeClass:printFunc()
    print("this is DerivativeClass`s printFunc")
end
 
return DerivativeClass

test:

local baseClass = require("BaseClass"):create()  -- 调用基类的默认构造函数ctor
baseClass:printFunc()  -- 调用基类的printFunc函数
 
local derivativeClass = require("DerivativeClass"):create()  -- 依次调用基类和派生类的默认构造函数
derivativeClass:printFunc()  -- 调用派生类的printFunc函数(重写基类的printFunc函数)
derivativeClass:testFunc()  -- 调用基类的testFunc函数

--[[
BaseClass:ctor
this is BaseClass`s printFunc
BaseClass:ctor
DerivativeClass:ctor
this is DerivativeClass`s printFunc
this is BaseClass`s testFunc
]]

附录

local setmetatableindex_
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
setmetatableindex = setmetatableindex_

Just Mark!

相关文章:

  • 不同种类的webservice错误信息
  • Lua中的require与package.loaded
  • 配置文件多个一个符号,导致struts抛出了匪夷所思的错误
  • 签名不对,请检查签名是否与开发平台上填写的一致
  • win终端工具Cmder的配置与使用
  • hibernate的lazy配置引起的问题
  • Lua实战之table.remove
  • 老婆说明书
  • cocos2d-lua:改变子节点优先级reorderChild
  • 几天工作中的三个技巧!
  • cocos2d-lua:动作Actions的使用
  • Lua实战之判断连续
  • Lua实战之统计字符串中任意字符出现的次数
  • 用OE收GMAIL信
  • cocos2d-lua:进度条ProgressTimer的使用
  • 【剑指offer】让抽象问题具体化
  • 30秒的PHP代码片段(1)数组 - Array
  • Angular 2 DI - IoC DI - 1
  • Django 博客开发教程 16 - 统计文章阅读量
  • gitlab-ci配置详解(一)
  • 老板让我十分钟上手nx-admin
  • 马上搞懂 GeoJSON
  • 阿里云服务器如何修改远程端口?
  • ​2020 年大前端技术趋势解读
  • ​iOS实时查看App运行日志
  • # C++之functional库用法整理
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • (solr系列:一)使用tomcat部署solr服务
  • (ZT)出版业改革:该死的死,该生的生
  • (差分)胡桃爱原石
  • (离散数学)逻辑连接词
  • (全注解开发)学习Spring-MVC的第三天
  • (五)IO流之ByteArrayInput/OutputStream
  • (一)基于IDEA的JAVA基础12
  • (转)我也是一只IT小小鸟
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .form文件_一篇文章学会文件上传
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .Net Web窗口页属性
  • .Net 中Partitioner static与dynamic的性能对比
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • .net分布式压力测试工具(Beetle.DT)
  • .NET技术成长路线架构图
  • .Net下的签名与混淆
  • @reference注解_Dubbo配置参考手册之dubbo:reference
  • @select 怎么写存储过程_你知道select语句和update语句分别是怎么执行的吗?
  • [ Linux ] git工具的基本使用(仓库的构建,提交)
  • [<MySQL优化总结>]
  • [04]Web前端进阶—JS伪数组
  • [2010-8-30]
  • [2019/05/17]解决springboot测试List接口时JSON传参异常
  • [Android]如何调试Native memory crash issue
  • [BUUCTF]-PWN:wustctf2020_number_game解析(补码,整数漏洞)
  • [C# 基础知识系列]专题十六:Linq介绍
  • [codevs 1296] 营业额统计