Guest User

basic_lua_classes

a guest
Jul 22nd, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. --
  2. --[[
  3.         by: Molinko
  4.         Create classes with multiple inheritance.
  5.         Keep it flippin' simple..
  6.     Updated: created var 'parents' in function 'Class' instead or the messy 'arg' table
  7.     -- thanks to 'theoriginalbit' for that catch.
  8. ]]
  9.  
  10. function Class( class, ... )
  11.         local parents = { ... }
  12.         class = class or {}
  13.         class.__index = class
  14.         class_mt = {
  15.                 -- look into list of parents for member
  16.                 __index = function( t, k )
  17.                     if #parents > 0 then
  18.                         for _, par in pairs( parents ) do
  19.                             if par[k] then
  20.                                 t[k] = par[k] -- store member for faster secondary lookup
  21.                                 return par[k]
  22.                             end
  23.                         end
  24.                     end
  25.                     return nil
  26.                 end,
  27.                 -- report class designation
  28.                 __tostring = function( t )
  29.                         return t.className
  30.                     end,
  31.                 -- create constructor metamethod. Usage: myClass( ... ), calls myClass:new( ... ).
  32.                 __call = function( t, ... )
  33.                         return t:new( ... )
  34.                     end,
  35.             }
  36.             -- Create instance constructor
  37.             function class:new( o )
  38.                     return setmetatable( o or {}, self )
  39.                 end
  40.             -- derp, derp
  41.             function class:getParents()
  42.                     return parents
  43.                 end
  44.            
  45.         return setmetatable( class, class_mt )
  46.     end
  47.    
  48.     -- Base class to inherit general methods for most classes
  49.     local Object = Class{ className = "Base_class_Object" }
  50.     function Object:printClass()
  51.             return "class: '" .. self.className .. "'"
  52.         end
  53.    
  54.     -- Person class
  55.     local Person = Class{ className = "Person_class" }
  56.     function Person:intro()
  57.             print( "Hello, my name is '" .. self.name .. "'" )
  58.         end
  59.    
  60.     -- Derived Class 'Wizard' inherits from parents: 'Object' & 'Person'
  61.     local Wizard = Class( { className = "Wizard_class" }, Object, Person )
  62.     --[[
  63.         Giving the class instances basic metamethods is easy enough. Don't overwrite the __index method though
  64.         as you will break the chain of inheritance.
  65.     ]]
  66.     Wizard.__tostring = function( t )
  67.             return t:printClass()
  68.         end
  69.        
  70.         -- Create class instance. optionally called with Wizard( ... ) using metamethod
  71.     local w1 = Wizard:new{ name = 'Paul, the amazing.' }
  72.    
  73.     -- testing
  74.     print(w1) -- calls Wizard.__tostring(), output --> "class: 'Wizard_class'"
  75.     w1:intro() -- output --> "Hello, my name is 'Paul, the amazing.'"
  76.     print(w1:getParents()) -- output --> table of Class parents
  77.     print(Wizard) -- calls Wizard_mt.__tostring() output --> 'Wizard_class'
  78.    
  79.    
  80.    
  81.    
  82.    
  83.    
  84.    
  85.    
  86.    
  87.    
  88.    
  89.    
  90.    
  91.    
  92.    
  93.    
  94.    
  95.    
  96.    
  97.     --
Advertisement
Add Comment
Please, Sign In to add comment