object.lua 819 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. local Object = {}
  2. Object.__index = Object
  3. function Object:new()
  4. end
  5. function Object:extend()
  6. local cls = {}
  7. for k, v in pairs(self) do
  8. if k:find("__") == 1 then
  9. cls[k] = v
  10. end
  11. end
  12. cls.__index = cls
  13. cls.super = self
  14. setmetatable(cls, self)
  15. return cls
  16. end
  17. function Object:implement(...)
  18. for _, cls in pairs({...}) do
  19. for k, v in pairs(cls) do
  20. if self[k] == nil and type(v) == "function" then
  21. self[k] = v
  22. end
  23. end
  24. end
  25. end
  26. function Object:is(T)
  27. local mt = getmetatable(self)
  28. while mt do
  29. if mt == T then
  30. return true
  31. end
  32. mt = getmetatable(mt)
  33. end
  34. return false
  35. end
  36. function Object:__tostring()
  37. return "Object"
  38. end
  39. function Object:__call(...)
  40. local obj = setmetatable({}, self)
  41. obj:new(...)
  42. return obj
  43. end
  44. return Object