( ) Lua.
, ( ), . CAT .
, Lua , Lua, . , Lua CAT . . , .
CAT, . Self. () : tableName:methodName(), Lua . . , CAT, self .
, CAT Car.
metaCar = { __index = Car }
-- this table will be used as the metatable for all instances of Car
-- Lua will look in Car for attributes it can't find in the instance
:
-- instance table is called mustang
-- setmetatable(mustang, metaCar)
Here is a general purpose function that creates new instance objects and sets a metatet for it. If CAT has a constructor function (init), it will also be executed.
function newObj(metatable)
..obj = {} -- create new empty instance object
..setmetatable(obj, metatable) –- connect the metatable to it
..if obj.init then -- if the CAT has an init method, execute it
....obj:init()
..end
..return obj
end
source
share