Lua元表的使用

来源:互联网 发布:怎么弄网络平台卖东西 编辑:程序博客网 时间:2024/04/28 18:43
Window = {}Window.prototype = {x = 0, y = 0, width = 100, height = 200,}Window.mt = {}function  Window.new( o )setmetatable( o, Window.mt )return oendWindow.mt.__index = function ( table, key )return Window.prototype[key]endw = Window.new{x = 10, y = 20}print(w.width)function setDefault( t, d )local mt = {__index = function() return d end}setmetatable( t, mt )endtab = {x = 10, y = 20 }print( tab.x, tab.y, tab.z )setDefault( tab, 0 )print( tab.x, tab.y, tab.z )

0 0
原创粉丝点击