Window = {}
Window.prototype = {x = 0, y = 0, width = 100, height = 200,}
Window.mt = {}
function Window.new( o )
setmetatable( o, Window.mt )
return o
end
Window.mt.__index = function ( table, key )
return Window.prototype[key]
end
w = Window.new{x = 10, y = 20}
print(w.width)
function setDefault( t, d )
local mt = {__index = function() return d end}
setmetatable( t, mt )
end
tab = {x = 10, y = 20 }
print( tab.x, tab.y, tab.z )
setDefault( tab, 0 )
print( tab.x, tab.y, tab.z )
Lua元表的使用
最新推荐文章于 2024-06-02 14:40:10 发布