{"id":258287,"date":"2015-05-29T21:56:02","date_gmt":"2015-05-29T17:56:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=258287"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=258287","title":{"rendered":"\u041f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u043d\u043e\u0435 \u041e\u041e\u041f \u0434\u043b\u044f Lua"},"content":{"rendered":"<p>     \t\u041f\u0440\u0438\u0432\u0435\u0442,<br \/>  \u044f \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b \u0441\u0432\u043e\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u041e\u041e\u041f \u0432 Lua.<\/p>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0438\u0448\u043a\u0438  <\/p>\n<ul>\n<li>Single inheritance<\/li>\n<li>Traits<\/li>\n<li>LuaJIT<\/li>\n<\/ul>\n<p>  \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c.<\/p>\n<pre><code class=\"lua\">-- \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u044c local object = require(&quot;object&quot;)  -- \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 local HelloClass = object:extend(function(class)   -- \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 (\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)   function class:init(name)     self.name = name   end    -- \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430   function class:sayHello()     print(&quot;Hello &quot; .. self.name)   end end)  local hello = HelloClass:new(&quot;John&quot;) hello:sayHello() <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u0432\u0441\u044f \u043c\u0430\u0433\u0438\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 extend(traits&#8230;, f), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<p>  \u041c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432\u043d\u0443\u0442\u0440\u0438<\/p>\n<pre><code class=\"lua\">local object = require(&quot;object&quot;) local Status = object:extend(function(status)   status.HTTP_200_OK = {200, &quot;OK&quot;}   status.HTTP_405_METHOD_NOT_ALLOWED = {404, &quot;Method not allowed&quot;} end)  print(Status.HTTP_200_OK[2]) <\/code><\/pre>\n<h5>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b<\/h5>\n<p>  \u041a\u0443\u0434\u0430 \u0436\u0435 \u0431\u0435\u0437 \u043d\u0438\u0445\u2026<\/p>\n<pre><code class=\"lua\">local object = require(&quot;object&quot;) local MathUtils = object:extend(function(class)  function class.square(x)   return x * x  end end)  -- \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 print(MathUtils.square(10))  -- \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u0442 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434 \u043d\u043e \u0443\u0436\u0435 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0441\u0442\u0430\u043d\u0441 print(MathUtils:new().square(10)) -- 100 <\/code><\/pre>\n<h5>\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/h5>\n<p>  \u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0447\u0435\u0440\u0435\u0437 new() \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 init()<\/p>\n<pre><code class=\"lua\">local Counter = object:extend(function(class)    -- \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 (\u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432)    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) <\/code><\/pre>\n<h5>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u0432<\/h5>\n<p>  \u041a\u0430\u043a \u044f \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b, \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043a\u0430\u043a single inheritance, \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u00ab\u043a\u043b\u0430\u0441\u0441\u0430\u00bb, \u043e\u0434\u043d\u0430 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0442\u0440\u0435\u0439\u0442\u044b, \u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0433\u043e\u0432\u0438\u043c \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435. \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<pre><code class=\"lua\">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   -- \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434   function class:getArea()     return self.side * self.side   end end)  local sq = Square:new(10) print(&quot;Area = &quot; .. sq:getArea()) <\/code><\/pre>\n<h5>\u0412\u044b\u0437\u043e\u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430<\/h5>\n<p>  \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u043b\u044f\u043c\u0431\u0434\u0430-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0435 \u0432 extend, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0435\u0441\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c)<\/p>\n<pre><code class=\"lua\">local Foo = object:extend(function(class)   function class:init(value)     self.value = value   end    function class:say()     print(&quot;Hello &quot; .. self.value)   end end)  class Bar = Foo:extend(function(class, parent)   function class:init(value)     -- \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f     parent.init(self, value)    end end)  local foo = Foo:new(&quot;World&quot;) foo:say() -- \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 &quot;Hello World&quot;  local bar = Bar:new(&quot;World&quot;) bar:say() -- \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 &quot;Hello World&quot; <\/code><\/pre>\n<h5>\u0422\u0440\u0435\u0439\u0442\u044b<\/h5>\n<p>  \u041a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u043a \u0432 \u0421++, \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0440\u0435\u0439\u0442\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b.<br \/>  \u041f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0439\u0442\u0435 \u0432\u0430\u0448\u0438 \u0442\u0440\u0435\u0439\u0442\u044b \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 extend()<\/p>\n<pre><code class=\"lua\">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() <\/code><\/pre>\n<h5>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h5>\n<p>  <i>is_instanceof(self, instance) <\/i> \u2014 \u0432\u0435\u0440\u043d\u0435\u0442 true \u0435\u0441\u043b\u0438 instance \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u044b\u043c \u0438\u043b\u0438 \u043d\u0435\u043f\u0440\u044f\u043c\u044b\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c self<\/p>\n<pre><code class=\"lua\">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 <\/code><\/pre>\n<p>  i<i>s_typeof(self, instance) <\/i> \u2014 \u0432\u0435\u0440\u043d\u0435\u0442 true \u0435\u0441\u043b\u0438 instance \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u044b\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c self <\/p>\n<pre><code class=\"lua\">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 <\/code><\/pre>\n<h5>LuaJIT<\/h5>\n<p>  \u041a\u0430\u043a \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0432 LuaJIT.<\/p>\n<h5>\u0413\u0434\u0435 \u043a\u043e\u0434, \u041a\u0430\u0440\u043b?<\/h5>\n<p>  <a href=\"https:\/\/github.com\/santeri-io\/lua-object\">\u0417\u0434\u0435\u0441\u044c<\/a>       \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/259145\/\"> http:\/\/habrahabr.ru\/post\/259145\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u041f\u0440\u0438\u0432\u0435\u0442,<br \/>  \u044f \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b \u0441\u0432\u043e\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u041e\u041e\u041f \u0432 Lua.<\/p>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0438\u0448\u043a\u0438  <\/p>\n<ul>\n<li>Single inheritance<\/li>\n<li>Traits<\/li>\n<li>LuaJIT<\/li>\n<\/ul>\n<p>  \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c.<\/p>\n<pre><code class=\"lua\">-- \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u044c local object = require(&quot;object&quot;)  -- \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 local HelloClass = object:extend(function(class)   -- \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 (\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)   function class:init(name)     self.name = name   end    -- \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430   function class:sayHello()     print(&quot;Hello &quot; .. self.name)   end end)  local hello = HelloClass:new(&quot;John&quot;) hello:sayHello() <\/code><\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-258287","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/258287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=258287"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/258287\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=258287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=258287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=258287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}