【lua】小计setmetatable

来源:互联网 发布:淘宝茶叶散装也需要qs 编辑:程序博客网 时间:2024/05/16 12:14
local base = {x = 5, y = 10, width = 100, height = 100}base.__index = basefunction base:new()   local obj = {}   setmetatable(obj, self)   return objendfunction base:Print()   print('x:' .. self.x)   print('y:'.. self.y)   print('width:' .. self.width)   print('height:'.. self.height)endlocal child = { color = 'red', size = 100 }child.__index = childsetmetatable(child, base)function child:new(color, size)   local obj = base:new()   setmetatable(obj, self)   obj.color = color   obj.size = size   return objendfunction child:Print()   print('x:' .. self.x)   print('y:'.. self.y)   print('width:' .. self.width)   print('height:'.. self.height)   print('color:' .. self.color)   print('size' .. self.size)endlocal c = child:new('blue', 1000)c:Print()local b = base:new()b:Print()



 

一直对lua中的metatable不是很清楚,今天试了试,并没有想象中那么难。

原创粉丝点击