SHOW:
|
|
- or go back to the newest paste.
1 | local _M = {} -- module table | |
2 | ||
3 | _M.someProperty = 1 -- class properties | |
4 | ||
5 | local function createText() | |
6 | -- local function are still valid, but not seen from outside - "private" | |
7 | end | |
8 | ||
9 | local privateVar -- so do local variables | |
10 | ||
11 | _GLOBAL_VAR -- without local it's global | |
12 | ||
13 | function _M.staticMethod(vars) | |
14 | -- this is class method like function (dot) | |
15 | -- there is no "self" | |
16 | end | |
17 | ||
18 | function _M:someMethod(vars) | |
19 | -- this is object method like function (colon) | |
20 | -- there is "self" | |
21 | end | |
22 | ||
23 | - | return _M -- return this table as a module to require() |
23 | + | function _M:newBaseObject() |
24 | -- Here goes constructor code | |
25 | - | return _M |
25 | + | local object = display.newImage(...) -- could be a display object or an empty table {} |
26 | object.vars = 'some vars' | |
27 | object.name = 'BaseObject' | |
28 | object.property = self.someProperty -- from module | |
29 | ||
30 | function object:sign(song) | |
31 | print(self.name .. ' is singing ' .. song) | |
32 | end | |
33 | ||
34 | ||
35 | function object:destroy() | |
36 | -- optional destructor, you can override removeSelf() as well | |
37 | self.vars = nil | |
38 | self:removeSelf() | |
39 | end | |
40 | ||
41 | return object | |
42 | end | |
43 | ||
44 | -- Now inheritance | |
45 | function _M:newChildObject() | |
46 | local object = _M:newBaseObject() | |
47 | -- override any methods or add new | |
48 | object.name = 'ChildObject' | |
49 | function object:tell(story) | |
50 | print(self.name .. ' is telling ' .. story) | |
51 | end | |
52 | return object | |
53 | end | |
54 | ||
55 | return _M -- return this table as a module to require() |