一 、table扩展

-- 返回table大小
table.size = function(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end -- 判断table是否为空
table.empty = function(t)
return not next(t)
end -- 返回table键列表
table.keys = function(hashtable)
local keys = {}
for k, v in pairs(hashtable) do
keys[#keys + 1] = k
end
return keys
end -- 返回table值列表
table.values = function(t)
local result = {}
for k, v in pairs(t) do
table.insert(result, v)
end
return result
end -- 返回索引
table.indexof = function(array, value, begin)
for i = begin or 1, #array do
if array[i] == value then return i end
end
return -1
end table.equal = function (a, b)
if type(a)~="table" or type(b)~="table" then
return false
end
if #a ~= #b then
return false
end for k, v in pairs(a) do
if b[k] == nil then
return false
end
if type(a[k]) == "table" then
if not table.equal(a[k], b[k]) then
return false
end
end
if a[k] ~= b[k] then
return false
end
end
return true
end -- 浅拷贝
table.clone = function(t, nometa)
local result = {}
if not nometa then
setmetatable(result, getmetatable(t))
end
for k, v in pairs (t) do
result[k] = v
end
return result
end -- 深拷贝
table.copy = function(t, nometa)
local result = {} if not nometa then
setmetatable(result, getmetatable(t))
end for k, v in pairs(t) do
if type(v) == "table" then
result[k] = copy(v)
else
result[k] = v
end
end
return result
end table.merge = function(dest, src)
for k, v in pairs(src) do
dest[k] = v
end
end table.shuffle = function(t)
if type(t)~="table" then
return
end
local tab={}
local index=1
while #t~=0 do
local n=math.random(0,#t)
if t[n]~=nil then
tab[index]=t[n]
table.remove(t,n)
index=index+1
end
end
return tab
end table.find = function (tab, value)
for k,v in ipairs(tab) do
if v == value then
return k
end
end
return nil
end

二、面向对象扩展

-- lua面向对象扩展
function class(classname, super)
local superType = type(super)
local cls if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end if superType == "function" or (super and super.__ctype == 1) then
-- inherited from native C++ Object
cls = {} if superType == "table" then
-- copy fields from super
for k,v in pairs(super) do cls[k] = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
cls.ctor = function() end
end cls.__cname = classname
cls.__ctype = 1 function cls.new(...)
local instance = cls.__create(...)
-- copy fields from class to native object
for k,v in pairs(cls) do instance[k] = v end
instance.class = cls
instance:ctor(...)
return instance
end else
-- inherited from Lua Object
if super then
cls = {}
setmetatable(cls, {__index = super})
cls.super = super
else
cls = {ctor = function() end}
end cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end return cls
end function iskindof(obj, classname)
local t = type(obj)
local mt
if t == "table" then
mt = getmetatable(obj)
elseif t == "userdata" then
mt = tolua.getpeer(obj)
end while mt do
if mt.__cname == classname then
return true
end
mt = mt.super
end return false
end

lua table面向对象扩展的更多相关文章

  1. Lua 之面向对象编程

    Lua 之面向对象编程 Lua并不是为面向对象而设计的一种语言,因此,仅从原生态语法上并不直接支持面向对象编程,但Lua的设计中仍然包含了很多面向对象的思想,理解它们也更有助于理解Lua自身的一些高级 ...

  2. lua table integer index 特性

    table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...

  3. 树形打印lua table表

    为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...

  4. lua table 排序--满足多条件排序

    前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...

  5. cocos2d-x lua table数据存储

    cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...

  6. cocos2d-x lua table与json的转换

    cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...

  7. Lua table使用

    days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...

  8. lua table表

    lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...

  9. lua table表判断是否为空

    官方手册里早已经给了答案,那就是靠lua内置的next函数 即如此用: a = {} if next(a) == nil then next其实就是pairs遍历table时用来取下一个内容的函数. ...

随机推荐

  1. Spring框架分为哪七大模块,各模块的主要功能作用是什么

    七大模块,如下: 1. Spring Core: Core封装包是框架的最基础部分,提供IOC和依赖注入特性.这里的基础概念是BeanFactory,它提供对Factory模式的经典实现来消除对程序性 ...

  2. 【云原生下离在线混部实践系列】深入浅出 Google Borg

    Google Borg 是资源调度管理和离在线混部领域的鼻祖,同时也是 Kubernetes 的起源与参照,已成为从业人员首要学习的典范.本文尝试管中窥豹,简单从<Large-scale clu ...

  3. Spring Boot 项目打成 war 包部署

    Spring Boot 一个非常方便的功能就是支持内置的 Servlet 容器,一般我们部署 Spring Boot 应用时都是打成一个可执行的 Jar 包进行部署.其实 Spring Boot 也是 ...

  4. Vue mustache语法

    mustache语法 Vue中的插值语法mustache本意为胡子,可能是{{}}长得像胡子吧. 下面是对mustache插值语法一个最简单的使用. 被管理元素会通过data属性拿到其中的数据对象. ...

  5. day61:Linux:权限管理&rpm软件包管理&yum工具

    目录 1.权限管理 2.rpm软件包管理 3.yum工具(联网) 权限管理 1.什么是权限? 权限主要用来约束用户能对系统所做的操作 2.为什么要使用权限? 因为系统中不可能只存在一个root用户,一 ...

  6. Go-归档文件-tar

    文件归档 tar 1. 创建一个tar头部并自动填充tar头部信息 tar.FileInfoHeader() 联合 os.Stat() 方法 2. 手动填写 tar头部信息 tar.Header{} ...

  7. 报表工具FastReport VCL 最新版发布!

    新功能 为主要包类添加了类引用 在报表设计器中添加了SQL编辑器的自定义 为TfrxReport的操作添加了延迟的命令池:PrepareReport,ShowReport,LoadFrom.可以调用R ...

  8. MyBatis 进阶,MyBatis-Plus!(基于 Springboot 演示)

    这一篇从一个入门的基本体验介绍,再到对于 CRUD 的一个详细介绍,在介绍过程中将涉及到的一些问题,例如逐渐策略,自动填充,乐观锁等内容说了一下,只选了一些重要的内容,还有一些没提及到,具体可以参考官 ...

  9. 小伙伴问我:如何搭建Maven私服?我连夜肝了这篇实战文章!!

    写在前面 十一假期期间,也有很多小伙伴不忘学习呀,看来有很多小伙伴想通过十一长假来提升自己的专业技能!这不,就有小伙伴在微信上问我:如何搭建Maven私服?让我专门推一篇搭建Maven私服的文章.安排 ...

  10. 04 C语言基本语法

    C语言的令牌 C 语言的程序代码由各种令牌组成,令牌可以是关键字.标识符.常量.字符串值,或者是一个符号.例如,下方的C语句包括5个令牌: printf("Hello, World! \n& ...