Difference between revisions of "class"

From Inspired-Lua Wiki
Jump to navigation Jump to search
m (moved class() to class)
 
Line 1: Line 1:
 
A complete tutorial about classes is written here: http://inspired-lua.org/index.php/2011/05/5-object-classes/
 
A complete tutorial about classes is written here: http://inspired-lua.org/index.php/2011/05/5-object-classes/
  
Here's how the class() function is defined, in the API :
+
Here's how the class([parent]) function is defined, in the API :
  
 
<source lang="lua">
 
<source lang="lua">

Latest revision as of 17:16, 2 August 2013

A complete tutorial about classes is written here: http://inspired-lua.org/index.php/2011/05/5-object-classes/

Here's how the class([parent]) function is defined, in the API :

class = function(prototype)
local derived={}
 	if prototype then
 		function derived.__index(t,key)
 			return rawget(derived,key) or prototype[key]
 		end
 	else
 		function derived.__index(t,key)
 			return rawget(derived,key)
 		end
 	end
 	function derived.__call(proto,...)
 		local instance={}
 		setmetatable(instance,proto)
 		local init=instance.init
 		if init then
 			init(instance,...)
 		end
 		return instance
 	end
 	setmetatable(derived,derived)
 	return derived
 end