lua metatable

400 词
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
t = setmetatable({ bar = 4, foo = 7 }, { __index = { foo = 3 } })

print(t.foo) – 7
print(t.bar) – 4

t = setmetatable({ }, { __index = { foo = 3} })

print(t.foo) – 3
print(t.bar) – nil

fuc = function (t,k)
if k == 'foo' then
return rawget(t, 'bar')
else
return 0
end
end

t = setmetatable({ }, { __index = fuc })

print(t.foo) – 3
print(t.bar) – nil
print(t.ff)