Lua Class

                Never    
Lua
       
createClass = function(...)
	-- "result" is the new class
	local result, bases = {}, {...}
	-- copy base class contents into the new class
	for i, base in ipairs(bases) do
		for k, v in pairs(base) do
			result[k] = v
		end
	end
	-- set the class's __index, and start filling an "is_a" table that contains this class and all of its bases
	-- so you can do an "instance of" check using my_instance.is_a[MyClass]
	result.__index, result.is_a = result, {[result] = true}
	for i, base in ipairs(bases) do
		for c in pairs(base.is_a) do
			result.is_a[c] = true
		end
		result.is_a[base] = true
	end
	-- the class's __call metamethod
	setmetatable(result, {
		__call = function (cls, ...)
			local instance = setmetatable({}, cls)
			-- run the init method if it's there
			local init = instance._init
			if init then
				init(instance, ...)
			end
			return instance
		end
	})
	-- return the new class table, that's ready to fill with methods
	return result
end

Panel = createClass()
function Panel:_init(x, y)
  self.x = x
  self.y = y
end
function Panel:Print()
    print("X: " .. self.x .. "; Y: " .. self.y)
end

panel = Panel(1,1)
panel:Print()

Raw Text