Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Woman = {};
--наследуемся
extended(Woman, Person)
Если кто-то путается где ставить точку, а где двоеточие, правило следующее: если обращаемся к свойству — ставим точку (obj.name), если к методу — ставим двоеточие (obj:getName()).
sixdays:new "room" () { room = true }
sixdays:new "player" () : moveto( room )
sixdays:new "apple_red" () { name = "red apple" } : moveto( room )
sixdays:new "apple_green" (){ name = "green apple" } : moveto( room )
sixdays:new "table_red" () { name = "red table" } : moveto( room )
sixdays:new "table_green" (){ name = "green table" } : moveto( room )
-------------------------------------------------------------
local Object = {}
function Object:new(properties)
properties = properties or {}
setmetatable(properties, self)
self.__index = self
return properties
end
function Object:inherit(properties)
return self:new():new(properties)
end
-------------------------------------------------------------
local Person = Object:inherit(
{
name = "default"
})
function Person:get_name()
return self.name
end
-------------------------------------------------------------
local Coconut = Person:inherit(
{
cosplay = "maiden"
})
function Coconut:get_name()
return "sexy " .. self.name
end
function Coconut:get_cosplay()
return self.cosplay
end
-------------------------------------------------------------
person1 = Person:new()
print("person1: name=" .. person1.name ..
"\tget_name()=" .. person1:get_name())
-------------------------------------------------------------
person2 = Person:new({name = "Human"})
print("person2: name=" .. person2.name ..
"\tget_name()=" .. person2:get_name())
-------------------------------------------------------------
coconut1 = Coconut:new()
print("coconut1: name=" .. coconut1.name ..
"\tget_name()=" .. coconut1:get_name() ..
"\tcosplay=" .. coconut1.cosplay ..
"\tget_cosplay()=" .. coconut1:get_cosplay())
-------------------------------------------------------------
coconut2 = Coconut:new({name = "Coconut", cosplay = "nurse"})
print("coconut2: name=" .. coconut2.name ..
"\tget_name()=" .. coconut2:get_name() ..
"\tcosplay=" .. coconut2.cosplay ..
"\tget_cosplay()=" .. coconut2:get_cosplay())
-------------------------------------------------------------
person1: name=default get_name()=default
person2: name=Human get_name()=Human
coconut1: name=default get_name()=sexy default cosplay=maiden get_cosplay()=maiden
coconut2: name=Coconut get_name()=sexy Coconut cosplay=nurse get_cosplay()=nurse
Lua, ООП и ничего лишнего