Lua 之table的测试

来源:互联网 发布:淘宝10元包邮在哪 编辑:程序博客网 时间:2024/06/05 17:34

一.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




0 0
原创粉丝点击