12. Class¶
Warning
This section contains advanced feature of Haka.
- object Class¶
The Haka class allows to define custom Lua object.
- class.class(name, parent=nil)¶
Parameters: - name (string) – Class name.
- parent (Class) – Parent class to inherit from.
Create a new class.
- <Class>:new(...) → instance¶
Returns: - instance (Class instance) – New instance of the class.
Create a new instance of the class.
- class.isa(instance, class)¶
Parameters: Check if the given instance is a class.
- class.classof(instance) → class¶
Parameters: - instance (Class instance) – Class instance to test.
Returns: - class (Class) – Class of this instance.
Get the class of a given instance.
- Class.method¶
Type: table List of methods available on instances of this class.
Special methods:
- __init: constructor function called every time a new instance of the class is created.
- __tostring: string conversion function for the class.
- __index: meta method __index
- __newindex: meta method __newindex
Example:
local A = class("A") function A.method:f() print("f()") end local a = A:new() a:f() -- will print 'f()'
- Class.property¶
Type: table List of properties on this class. A property is a table containing two functions:
- class.get(self) → value¶
Getter for the property. The function is called every time the user access the property on the object.
- class.set(self, newvalue)¶
Setter for the property. It is called every time the user sets a new value on the property.
Example:
local A = class("A") function A.method:__init() self._prop = "my prop" end A.property:prop = { function get(self) return self._prop end, function set(self, value) self._prop = value end end local a = A:new() a.prop -- will print "my prop" a.prop = "foo bar"
- <Class>:addproperty(get, set)¶
Parameters: - get (function) – Getter for the property.
- set (function) – Setter for the property.
Add a dynamic property to the class instance. This property can be accessed like any other property but only exists on this instance.