Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function simpleclass(base)
- return setmetatable(base, {
- __call = function(t, ...)
- local obj = {}
- if rawget(t, '__new') then
- t.__new(obj, ...)
- end
- return setmetatable(obj, t)
- end
- })
- end
- nn = simpleclass({
- -- constructor
- __new = function(self, name1)
- print("__new"..name1)
- end,
- -- destructor
- __gc = function(self)
- print("run __gc")
- end,
- -- metamethods
- __len = function(self)
- print("run __len")
- return 100
- end,
- __tostring = function(self)
- print("run __tostring")
- return "nn"
- end,
- -- methods, properties
- __index = {
- p1 = 'p1p1',
- p2 = 'p2p2',
- m1 = function(s)
- print("m1")
- end,
- }
- })
- aa = nn('aaa')
- print(nn.m1)
- print(aa.m1, aa, #aa)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement