从表成员中引用Lua表成员,可以使用以下方法:
local table_name = {
key1 = "value1",
key2 = "value2"
}
local value1 = table_name.key1
local table_name = {
key1 = "value1",
key2 = "value2"
}
local value1 = table_name["key1"]
local table_name = {
nested_table = {
key1 = "value1",
key2 = "value2"
}
}
local value1 = table_name.nested_table.key1
local value2 = table_name["nested_table"].key2
local table_name = {
key1 = "value1",
key2 = "value2"
}
local key_name = "key1"
local value1 = table_name[key_name]
rawget
函数引用表成员:local table_name = {
key1 = "value1",
key2 = "value2"
}
local value1 = rawget(table_name, "key1")
pairs
函数遍历表并引用成员:local table_name = {
key1 = "value1",
key2 = "value2"
}
for key, value in pairs(table_name) do
print(key .. ": " .. value)
end
在这些方法中,可以根据需要选择合适的方式引用Lua表成员。
领取专属 10元无门槛券
手把手带您无忧上云