您的位置:首页 > 编程语言 > Lua

Lua 之table的测试

2015-07-06 10:21 459 查看

一.lua 的 table构造

1.字符串作为table索引

法1.

MyTable = {x = "xixi"}

法2.

MyTable = {["x"] = "xixi"}

法3.

MyTable = {}

MyTable.x = "xixi"

法4.

MyTable = {}

MyTable["x"] = "xixi"

注:

当使用数字字符串作为索引的时候(如:“10”):

法1(错误):MyTable = {10 = "xixi"}

法2:MyTable = {["10"] = "xixi"]}

法3(错误):MyTable = {} MyTable.10 = "xixi"

法4:MyTable = {} MyTable["10"] = "xixi"

2.数字作为table索引

法1.

MyTable = {[2] = "xixi"}

法2.

MyTable = {}

MyTable[2] = "xixi"

法3.

MyTable = {"x","y","z"}

这时候默认起始坐标为1,所以值为"y"对应的是MyTable[2]

3.字符串作为table索引,值为函数

法1.

x = {func =

function ()

print("x func call")

end

}

法2.

y = {["func"] =

function ()

print("y func call")

end

}

法3.

z = {}

z.func =

function ()

print("z func call")

end

法4.

w = {}

w["func"] =

function ()

print("w func call")

end

法5.(新增定义方法)

m = {}

function m.func()

print("m func call")

end

二.访问table的方法

法1.

MyTable[index]

法2.

MyTable.index



三.混合记录和列表风格的table构造式

polyline = {color = "blue",thickness = 2, "test",

{x=0,y=0},

{x=-10,y=10}

}

print(polyline.color) --blue

print(polyline[1]) --test

print(polyline[2].x) --0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: