lua-table使用笔记

参考:https://www.cnblogs.com/Dong-Forward/p/6063365.html

1:lua的table你可以先理解为既是一个数组也是一个字典

2:table判空

function isTableEmpty (t)
    if t == nil or next (t) == nil then
        return true
    else
        return false
    end
end

3:元表和__index

先是元表:

上一个基本代码example1.lua

local t1 = {name = "percy"}

local t2 = {age = "18"}

local t3 = setmetatable(t1, t2) 

print(t1.age)

print(t2.name)

print(t3.name)

print(t3.age)

-- conclusion: 返回值是他t1, t1, t2 没有被改变
-- 元表是为了来做一些操作符重载,还有其他工作的

print("------")
local mt = {}

mt.__add = function(a, b)
    return (a.v + b.v) / 2 
end

local t1 = {v = 10, v1 = 12} 
local t2 = {v = 20, v2 = 16} 

setmetatable(t1, mt) 

print("a + b =", t1 + t2)

再是__index, example2.lua

local mt = {}

mt.__index = function(table, key)
    print("call function with table and key")
end

mt.__newindex = function(t,k,v)
    print("call new index function")
end

local t = {v = 15} 

setmetatable(t, mt) 


local temp = t.v 
print(temp)
temp = t.a 

print(temp)

print("----")
t.a = 20

"index": 索引 table[key]。 当 table 不是表或是表 table 中不存在key 这个键时,这个事件被触发。 此时,会读出 table 相应的元方法。尽管名字取成这样, 这个事件的元方法其实可以是一个函数也可以是一张表。 如果它是一个函数,则以 table 和 key 作为参数调用它。如果它是一张表,最终的结果就是以 key 取索引这张表的结果。(这个索引过程是走常规的流程,而不是直接索引, 所以这次索引有可能引发另一次元方法。

"newindex": 索引赋值 table[key] = value 。 和索引事件类似,它发生在 table 不是表或是表 table 中不存在 key 这个键的时候。 此时,会读出 table 相应的元方法。同索引过程那样, 这个事件的元方法即可以是函数,也可以是一张表。 如果是一个函数, 则以 table、 key、以及 value 为参数传入。如果是一张表, Lua 对这张表做索引赋值操作。 (这个索引过程是走常规的流程,而不是直接索引赋值, 所以这次索引赋值有可能引发另一次元方法。)一旦有了 "newindex" 元方法, Lua 就不再做最初的赋值操作。(如果有必要,在元方法内部可以调用 rawset 来做赋值。

4:面向对象

-- Meta class
Shape = {area = 0}

-- 基础类方法 new
function Shape:new (o,side)
  o = o or {}
-- 这有点像运行时绑定,在后面对self的修改,这个o都能接受到
  setmetatable(o, self)
  self.__index = self
  side = side or 0
  self.area = side*side;
  return o
end

-- 基础类方法 printArea
function Shape:printArea ()
  print("面积为 ",self.area)
end

-- 创建对象
myshape = Shape:new(nil,10)

myshape:printArea()

print(myshape.area)

运行时绑定元表的证明

local mt = {v = 10}

local mtt = {b = 12}

--mt.__index = {a = 12, b = 20}
mt.__index = mtt

mtt.a = 15

local t = {c = 1}

setmetatable(t, mt)

local temp = t.a

print(temp)

5:mt.__index = mt

local mt = {v = 10} 

mt.__index = mt

mt.vv = 4 

local t = {a = 20} 

setmetatable(t, mt) 

local temp = t.vv
print(temp)

print("------")

local mt1 = {v = 10} 

mt.__index = function(table, key)
    print("call function in __index")
end

mt.vv = 4 

local t = {a = 20} 

setmetatable(t, mt) 

temp = t.vv
print(temp)

6:继承

http://www.runoob.com/lua/lua-object-oriented.html

7:table作为函数参数时,传递的是引用,在函数内对table的修改会映射到函数外的table,或者说一个table的赋值操作,也是一个引用传递

local t = {a = 1, b = 2}
local tt = t
tt.a = 3
print(t.a)

--result
--3

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值