-- local object = require("object") -- , local HelloClass = object:extend(function(class) -- () function class:init(name) self.name = name end -- function class:sayHello() print("Hello " .. self.name) end end) local hello = HelloClass:new("John") hello:sayHello()
local object = require("object") local Status = object:extend(function(status) status.HTTP_200_OK = {200, "OK"} status.HTTP_405_METHOD_NOT_ALLOWED = {404, "Method not allowed"} end) print(Status.HTTP_200_OK[2])
local object = require("object") local MathUtils = object:extend(function(class) function class.square(x) return x * x end end) -- print(MathUtils.square(10)) -- print(MathUtils:new().square(10)) -- 100
local Counter = object:extend(function(class) -- , - ( ) function class:init(initial) self.ticks = initial or 0 end function class:tick() self.ticks = self.ticks + 1 end function class:getTicks() return self.ticks end end) local c = Counter:new() c.tick() c.tick() print(c:getTicks() == 2)
local Shape = object:extend(function(class) function class:getArea() return 0 end end) local Square = Shape:extend(function(class) function class:init(side) self.side = side end -- function class:getArea() return self.side * self.side end end) local sq = Square:new(10) print("Area = " .. sq:getArea())
local Foo = object:extend(function(class) function class:init(value) self.value = value end function class:say() print("Hello " .. self.value) end end) class Bar = Foo:extend(function(class, parent) function class:init(value) -- parent.init(self, value) end end) local foo = Foo:new("World") foo:say() -- "Hello World" local bar = Bar:new("World") bar:say() -- "Hello World"
local TraitX = function(trait) function trait:setX(x) self.x = x return self end function trait:getX() return self.x end end local A = object:extend(TraitX, function(class) function class:say() print(self.x) end end) A:new():setX(10):say()
local ClassA = object:extend() local ClassB = object:extend() local obj_a = ClassA:new() local obj_b = ClassB:new() print(obj_a:is_instanceof(ClassA)) -- true print(obj_a:is_instanceof(object)) -- true print(obj_a:is_instanceof(ClassB)) -- false
local ClassA = object:extend() local ClassB = object:extend() local obj_a = ClassA:new() local obj_b = ClassB:new() print(obj_b:is_typeof(ClassA)) -- false print(obj_b:is_typeof(ClassB)) -- true
Source: https://habr.com/ru/post/259145/
All Articles