Guest User

Untitled

a guest
Oct 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. function inheritsFrom( baseClass )
  2.  
  3. -- The following lines are equivalent to the SimpleClass example:
  4.  
  5. -- Create the table and metatable representing the class.
  6. local new_class = {}
  7. local class_mt = { __index = new_class }
  8.  
  9. -- Note that this function uses class_mt as an upvalue, so every instance
  10. -- of the class will share the same metatable.
  11. --
  12. function new_class:create()
  13. local newinst = {}
  14. setmetatable( newinst, class_mt )
  15. return newinst
  16. end
  17.  
  18. -- The following is the key to implementing inheritance:
  19.  
  20. -- The __index member of the new class's metatable references the
  21. -- base class. This implies that all methods of the base class will
  22. -- be exposed to the sub-class, and that the sub-class can override
  23. -- any of these methods.
  24. --
  25. if baseClass then
  26. setmetatable( new_class, { __index = baseClass } )
  27. end
  28.  
  29. return new_class
  30. end
Add Comment
Please, Sign In to add comment