我已經在互聯網上瀏覽了 3 天來找到這個問題的解釋,但是我沒有得到解決這個問題的解釋。無論如何,我無法解決為什么“gaming.child.p”回傳0而不是11。我將非常感謝您的解釋。
這是代碼:
local X = {
p = 1,
diffElementPls = "man"
};
local Y = {__index = X};
function interface()
local o = {};
return setmetatable(o, Y);
end
local w = {
something = "u found me",
child = interface()
};
local noob = {__index = w};
function new()
local o = {};
return setmetatable(o, noob);
end
local gaming = new();
local wd = new()
gaming.something = "noob"
gaming.child.p = 10
wd.child.p = 0
print(gaming.something, wd.something, gaming.child.p, wd.child.p) -- print noob u found me 0 0 instead of noob u found me 11 0
uj5u.com熱心網友回復:
gaming.child并wd.child參考同一張表。
因此修改wd.child.p也會改變 的值,gaming.child.p因為它是同一個變數。
兩者wd都gaming沒有欄位child。因此,當您索引它時,Lua 將參考它們的 metatable w。所以在這兩種情況下,你實際上是在修改w.child.p
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/446825.html
下一篇:線性資料結構
