S
Size: a a a
S
S
TC
S
PG
ШТ
--local, можно в произвольный с ключом
--tree
ШТ
local Object = {}
Object.__index = Object
function Object:new(num)
local o = { n = num }
setmetatable(o, Object)
return o
end
function Object:action()
print("Object:action", self.n)
end
local Button = {}
function Button:new(num)
local o = { n = num }
setmetatable(o, self)
self.__index = function(self, key)
return Button[key] or Object[key]
end
return o
end
function Button:zoom()
print("Button:zoom()", self.n)
end
local o = Object:new(100)
o:action()
local b = Button:new(10)
b:zoom()
b:action()
S
local Object = {}
Object.__index = Object
function Object:new(num)
local o = { n = num }
setmetatable(o, Object)
return o
end
function Object:action()
print("Object:action", self.n)
end
local Button = {}
function Button:new(num)
local o = { n = num }
setmetatable(o, self)
self.__index = function(self, key)
return Button[key] or Object[key]
end
return o
end
function Button:zoom()
print("Button:zoom()", self.n)
end
local o = Object:new(100)
o:action()
local b = Button:new(10)
b:zoom()
b:action()
self.__index = function(...) end
— проще повесить метатаблицу с __index на саму Button. Быстрее работать будет, базарю, инфа сотка.local Button = setmetatable({}, {__index = Object})
или дажеlocal Button = setmetatable({}, Object)
, если методы не перегружены и Object
прописан в себе самом как __index
.S
local Object = {}
Object.__index = Object
function Object:new(num)
local o = { n = num }
setmetatable(o, Object)
return o
end
function Object:action()
print("Object:action", self.n)
end
local Button = {}
function Button:new(num)
local o = { n = num }
setmetatable(o, self)
self.__index = function(self, key)
return Button[key] or Object[key]
end
return o
end
function Button:zoom()
print("Button:zoom()", self.n)
end
local o = Object:new(100)
o:action()
local b = Button:new(10)
b:zoom()
b:action()
S
self.__index
стоит вывести наружу как Button:__index
, определить её один раз и не перетирать-пересоздавать заново с каждым новым объектом. А то не заjit
уется в случае луажыта, и просто сгенерит кучу мусора в случае PUC Lua.ШТ
Button.__index = setmetatable(Button, { __index = Object })- то, что нужно! Доходит по-тихоньку
MG
IN
ВЗ
IN
IB