SHOW:
|
|
- or go back to the newest paste.
| 1 | --[[ | |
| 2 | ||
| 3 | class.lua | |
| 4 | - | Version: 1.0.2 |
| 4 | + | Version: 1.0.3 |
| 5 | ||
| 6 | Author: MegaLoler | |
| 7 | Email: [email protected] | |
| 8 | ||
| 9 | Contains functions for easily defining and instantiating classes in Lua. | |
| 10 | ||
| 11 | First make sure you have this in your code: | |
| 12 | require("class.lua")
| |
| 13 | ||
| 14 | Define a new class with the newClass() function: | |
| 15 | NewClass = newClass() | |
| 16 | ||
| 17 | Instantiate an new instance of the class with the new() method: | |
| 18 | newInstance = NewClass:new() | |
| 19 | ||
| 20 | Add methods to the class like this: | |
| 21 | function NewClass:newMethod() | |
| 22 | print("New method")
| |
| 23 | end | |
| 24 | ||
| 25 | Call methods of an instance of a class like this: | |
| 26 | newInstance:newMethod() -- prints "New method" | |
| 27 | ||
| 28 | Add a constructor to the class like this: | |
| 29 | function NewClass:init() | |
| 30 | print("New instance created")
| |
| 31 | end | |
| 32 | ||
| 33 | newInstance2 = NewClass:new() -- prints "New instance created" | |
| 34 | ||
| 35 | Use constructor parameters like this: | |
| 36 | function NewClass:init(par) | |
| 37 | print("New instance: " .. par)
| |
| 38 | end | |
| 39 | ||
| 40 | newInstance3 = NewClass:new("hello") -- prints "New instance: hello"
| |
| 41 | ||
| 42 | Create subclasses like this: | |
| 43 | Subclass = newClass(NewClass) | |
| 44 | ||
| 45 | subclassInstance = Subclass:new("hi") -- prints "New instance: hi"
| |
| 46 | ||
| 47 | Get the class of an instance like this: | |
| 48 | - | subclassInstance.getClass() |
| 48 | + | subclassInstance:getClass() |
| 49 | ||
| 50 | Get the superclass of an instance like this: | |
| 51 | subclassInstance:getSuperClass() | |
| 52 | ||
| 53 | Call a method of a superclass that has been overridden in its subclass like this: | |
| 54 | subclassInstance:getSuperClass():originalMethod() | |
| 55 | ||
| 56 | --]] | |
| 57 | ||
| 58 | function newClass(parent) | |
| 59 | local class = {} -- Method table
| |
| 60 | if Class then -- Class class doesn't inherit from anything | |
| 61 | setmetatable(class, {__index = parent or Class}) -- Inherit from parent, Class by default
| |
| 62 | end | |
| 63 | return class | |
| 64 | end | |
| 65 | ||
| 66 | Class = newClass() -- Create Class class | |
| 67 | ||
| 68 | function Class:new(...) -- Class constructor | |
| 69 | local instance = setmetatable({}, {__index = self})
| |
| 70 | instance:init(...) | |
| 71 | return instance | |
| 72 | end | |
| 73 | ||
| 74 | function Class:init() -- Instance constructor | |
| 75 | end | |
| 76 | ||
| 77 | function Class:getClass() -- Returns the class of an instance | |
| 78 | if Class == self then return nil end | |
| 79 | return getmetatable(self).__index | |
| 80 | end | |
| 81 | ||
| 82 | function Class:getSuperClass() -- Returns the superclass of an instance | |
| 83 | return self:getClass():getClass() | |
| 84 | end |