Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- main.lua --
- Window = require "module"
- w = Window.new( 10, _, 200 ) -- Задаем значения переменным: x = 10, width = 200.
- io.write( "x = " .. w.x, "\ny = " .. w.y, "\nwidth = " .. w.width, "\nheight = " .. w.height, "\n\n" ) -- Выводим поля 'w' по имени.
- for k, v in next, w do
- print( k .. " = " .. v ) -- Выводим все поля 'w'.
- end
- -- В первом выводе по имени, отсутствующие поля 'w' вызываются из таблицы-шаблона Window.prototype, за счет индексации его атрибутов с помощью метатаблицы 'Window.mt = { __index = Window.prototype }'.
- -- Во втором случае тупо выводятся атрибуты 'w'.
- -- module --
- local Window = { }
- Window.prototype = { x = 0, y = 0, width = 100, height = 100, }
- Window.mt = { __index = Window.prototype }
- local print = print
- local setmetatable = setmetatable
- _ENV = Window
- function new( ... )
- local inst = { }
- inst.x, inst.y, inst.width, inst.height = ...
- setmetatable( inst, Window.mt )
- return inst
- end
- return Window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement