Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. function simpleclass(base)
  2. return setmetatable(base, {
  3. __call = function(t, ...)
  4. local obj = {}
  5. if rawget(t, '__new') then
  6. t.__new(obj, ...)
  7. end
  8. return setmetatable(obj, t)
  9. end
  10. })
  11. end
  12.  
  13. nn = simpleclass({
  14. -- constructor
  15. __new = function(self, name1)
  16. print("__new"..name1)
  17. end,
  18.  
  19. -- destructor
  20. __gc = function(self)
  21. print("run __gc")
  22. end,
  23.  
  24. -- metamethods
  25. __len = function(self)
  26. print("run __len")
  27. return 100
  28. end,
  29. __tostring = function(self)
  30. print("run __tostring")
  31. return "nn"
  32. end,
  33.  
  34. -- methods, properties
  35. __index = {
  36. p1 = 'p1p1',
  37. p2 = 'p2p2',
  38.  
  39. m1 = function(s)
  40. print("m1")
  41. end,
  42. }
  43. })
  44.  
  45. aa = nn('aaa')
  46. print(nn.m1)
  47. print(aa.m1, aa, #aa)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement