Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. -- main.lua --
  2.  
  3. Window = require "module"
  4.  
  5. w = Window.new( 10, _, 200 ) -- Задаем значения переменным: x = 10, width = 200.
  6. io.write( "x = " .. w.x, "\ny = " .. w.y, "\nwidth = " .. w.width, "\nheight = " .. w.height, "\n\n" ) -- Выводим поля 'w' по имени.
  7.  
  8. for k, v in next, w do
  9.   print( k .. " = " .. v ) -- Выводим все поля 'w'.
  10. end
  11.  
  12. -- В первом выводе по имени, отсутствующие поля 'w' вызываются из таблицы-шаблона Window.prototype, за счет индексации его атрибутов с помощью метатаблицы 'Window.mt = { __index = Window.prototype }'.
  13.  
  14. -- Во втором случае тупо выводятся атрибуты 'w'.
  15.  
  16. -- module --
  17.  
  18. local Window = { }
  19. Window.prototype = { x = 0, y = 0, width = 100, height = 100, }
  20. Window.mt = { __index = Window.prototype }
  21.  
  22. local print = print
  23. local setmetatable = setmetatable
  24.  
  25. _ENV = Window
  26.  
  27. function new( ... )
  28.   local inst = { }
  29.   inst.x, inst.y, inst.width, inst.height = ...
  30.   setmetatable( inst, Window.mt )
  31.   return inst
  32. end
  33.  
  34. return Window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement