Lua??????????????(metatable)???????
???????????? ???????[ 2016/4/12 13:41:59 ] ??????????????? ???????
????Lua??????????????????????????????????????????????????????á???????????Lua??????????table??????????????????????????????????????????table?С??磺
-- file:test.lua
local test = {}
function test:get_x()
return self.x or 0
end
function test:set_x( _x )
self.x = _x
end
local test_module = {}
function test_module.new()
local t = {}
for k??v in pairs( test ) do
t[k] = v
end
return t
end
return test_module
???????????????
????-- file:main.lua
????local test = require "test"
????local _t = test.new()
????_t:set_x( 999 )
????print( _t:get_x() )
???????????????????????????????????????д??Щ???
????1.???????????????(???????????????C++???????)
????2.????????????????????????????
????3.??????
????Lua?????????(metatable)?????????????????????????£?
????1.?????????????????????
-- file:oo.lua
local oo = {}
local cls = {}
local function new( clz )
local t = {}
setmetatable(t?? clz)
return t
end
function oo.class( parent??name )
local t = {}
cls[name] = t
parent = parent or {}
rawset( t??"__index"??t )
setmetatable( t??{ __index = parent??__call = new } )
return t
end
return oo
????2.???????д???????
-- file:test.lua
local oo = require "oo"
local test = oo.class( nil??... )
function test:get_x()
return self.x or 0
end
function test:set_x( _x )
self.x = _x
end
return test
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11