lua table面向对象扩展
一 、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面向对象扩展的更多相关文章
- Lua 之面向对象编程
Lua 之面向对象编程 Lua并不是为面向对象而设计的一种语言,因此,仅从原生态语法上并不直接支持面向对象编程,但Lua的设计中仍然包含了很多面向对象的思想,理解它们也更有助于理解Lua自身的一些高级 ...
- lua table integer index 特性
table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...
- 树形打印lua table表
为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...
- lua table 排序--满足多条件排序
前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...
- cocos2d-x lua table数据存储
cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...
- cocos2d-x lua table与json的转换
cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...
- Lua table使用
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...
- lua table表
lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...
- lua table表判断是否为空
官方手册里早已经给了答案,那就是靠lua内置的next函数 即如此用: a = {} if next(a) == nil then next其实就是pairs遍历table时用来取下一个内容的函数. ...
随机推荐
- HTML+CSS使用swiper快速生成最简单、最快捷、最易看懂的轮播图
1. 在网页顶部输入swiper.com.con,进入swiper官网 2. 点击" API文档",获取轮播图代码的地方 3. 点击左侧"swiper初始化&q ...
- 快速上手开发——JFinal配置(全步骤图文解析)
摘要: 因为发现官网上只有Eclipse的配置文档,就写了这篇基于IDEA+maven的配置流程.本文使用安装了maven插件的IDEA进行配置,为了照顾IDEA新手,几乎每个步骤都截了图. 环境说明 ...
- js实现表单验证
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Flutter中如何方便的获取音视频的长度
此次主要是flutter集成im,在发送视频时需要加上时长,但是用视频controller只能在初始化时具备路径才可以可以使用:just_audio插件中的方法进行获取 详情看官方文档:https:/ ...
- 记一次公司mssql server密码频繁被改的事件
环境描述 近期公司服务器mssql密码频繁被改,导致各种业务系统无法连接,报错.昨天来公司,发现4台数据库3台密码都变了.今天尝试着去查查是否能找到问题根源. 步骤 4台服务器3台连不上,只有64还活 ...
- MySQL 8.0索引合并
简介 参考https://dev.mysql.com/doc/refman/8.0/en/index-merge-optimization.html#index-merge-intersection. ...
- MarkDown系列教程
编辑了一个Markdown的系列教程,前一部分是摘编自 菜鸟教程 网站 目录 第一篇 Markdown 使用教程 入门
- Intel HEX格式
来来 !! come baby ! 只强调一点这篇文章有checksum的算法,是我最喜欢地!! 参考:https://blog.csdn.net/extlife/article/details/ ...
- TP5 调用快递鸟api 查询快递信息
1,去快递鸟,下载sdk https://www.kdniao.com/api-track 下载PHPsdk 2,下载下来的事PHP文件,不是以类的形式显示的,所以为了方便,我把他封装成了类,不需要封 ...
- 本溪6397.7539(薇)xiaojie:本溪哪里有xiaomei
本溪哪里有小姐服务大保健[微信:6397.7539倩儿小妹[本溪叫小姐服务√o服务微信:6397.7539倩儿小妹[本溪叫小姐服务][十微信:6397.7539倩儿小妹][本溪叫小姐包夜服务][十微信 ...