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时用来取下一个内容的函数. ...
随机推荐
- PYG2010-01-软件破解意义和程序流程简介笔记
- 【Processing-日常3】等待动画1
之前在CSDN上发表过: https://blog.csdn.net/fddxsyf123/article/details/79755976
- 对于filter的理解
filter语法:使用filter会创建一个新数组,所以原数组不变 array.filter(function(value,index,arr), thisValue) 其中:arr:数组(可选) i ...
- 在uniapp或者vue中单行文字或者符号无法换行的终极解决方案
在VUE开发过程中,会出现比较诡异的情况. 比如常规的英文或中文显示都是很正常的,但是当出现了一些中文符号(比如,!等等)在文末的时候,总是会超出view的显示区域. 那么在遇到上面这种问题我们记得检 ...
- Camera插件之CinematicCamera介绍
Camera插件之CinematicCamera 继承自PerspectiveCame 构造函数引用的PerspectiveCamera的构造函数, 代码如下 var CinematicCamera ...
- 2020武汉dotNET俱乐部分享交流活动正式启动
去年9月去上海参加了2019 .NET开发者峰会,感触良多.回来后便一直想着在武汉也组织一场这样的活动,推动一下武汉.NET的发展.由于疫情的影响,这个想法一直被搁浅,好在疫情总算是控制住了,所以我们 ...
- ubuntu20 使用命令安装 rabbitmq
安装 rabbitmq sudo apt-get install erlang-nox -y sudo apt-get update sudo apt-get install rabbitmq-ser ...
- devops-jenkins-Pipeline基础语法
1. jenkins-Pipeline基础语法 1) jenkins-Pipeline总体介绍 • Pipeline,简而言之,就是一套运行与jenkins上的工作流框架,将原本独立运行于单个或多个 ...
- docker启动镜像报错
docker启动镜像报错: docker: Error response from daemon: driver failed programming external connectivity on ...
- 多测师_讲解python__004 函数
# 函数:一个工具,随调随用# 降级代码冗余## 增加代码的复用性,提高开发效率,为了不成为cv战士## 提高程序扩展性## 函数有两个阶段:定义阶段,调用阶段.## 定义时:只检查函数体内代码语法, ...