Yet another class library for Lua, because there aren't enough already. ¯\_(ツ)_/¯
It follows a small example showing its basic usage.
Personally I use PascalCase for my class names, which is fairly common in other languages as well. Of course, feel free to use whichever convention suits you best.
local class = require "class"
-- Creating the class:
local Value = class("Value")
function Value:create(value)
self._value = value
end
function Value:get()
return self._value
end
function Value:set(value)
self._value = value
end
-- Creating an instance:
local value = Value("Hello World!")
print(value:get()) --> Hello World!
value:set("How are you?")
print(value:get()) --> How are you?
A class Value
with simple create
(constructor), get
and set
methods.
I generally put each class in its own file and name the file accordingly, similar to how Object.lua and Event.lua look.
- The core library.
- Can be used standalone.
- Supports creation of classes and inheritance.
- Can be used as a base class.
- Enables the use of properties using custom
get
andset
functions. - Comes with useful helper functions to create properties with common
get
andset
functions.
- Provides a simple multicast event.
- Handlers can be added and removed with the
add
andremove
methods. - Calls all handlers in the order they were added, when the event is called.
- Shows basic usage.
- A simple class
Value
withValue:get()
andValue:set(value)
methods.
- Customize instance creation with a
MyClass:create()
method. - Creating instance fields.
- Creating a base class
Animal
and a subclassCat
. - Virtual and abstract methods.
- Calling base methods in a derived method.
- Checking if something is a class or an object using
class.isClass()
andclass.isObject()
. - Safely turning any value into its class (or
nil
) usingclass.of()
. - Checking the inheritance hierachy of classes and objects using the comparison operators.
- Customize what happens, when a certain class is inherited from.