API
Before using Classy you must require
the plugin in your code file:
local Classy = require("plugin.classy")
Classy
create
Create a new base class. See also Classes in the Usage guide.
Classy.create(className[, defaults])
Arguments
Name | Description | Type | Required |
---|---|---|---|
className | The name of the class. | String | Y |
defaults | A key/value table of default properties. | Table | N |
Returns
A new base class.
Example
Basic
local Person = Classy.create("Person")
Defaults
local Person = Classy.create("Person", { age = 18 })
isClass
Utility function to check if an object is a base class or subclass.
Classy.isClass(object)
Returns
A Boolean value.
Example
local Person = Classy.create("Person") print(Classy.isClass(Person)) --> true
isInstance
Utility function to check if an object is an instance of a class or subclass.
Classy.isInstance(object)
Returns
A Boolean value.
Example
local person = Person:new() print(Classy.isInstance(person)) --> true
Classes
constructor
An optional constructor function that is called when creating new instances of a class (or subclass).
<className>:constructor([args])
Arguments
Name | Description | Type | Required |
---|---|---|---|
args | Common delimited set of arguments to be consumed when a new instance of the class (or subclass) is created. See new. | Various | N |
Returns
This method has no return value.
Example
function Person:constructor(name, age) self.name = name self.age = age end
Notes
- The passed arguments can be of any Lua type.
extends
Creates a subclass of a base class (or subclass). See also Subclasses in the Usage guide.
<className>:extends(subclassName[, defaults])
Arguments
Name | Description | Type | Required |
---|---|---|---|
subclassName | The name of the subclass. | String | Y |
defaults | A key/value table of default properties. | Table | N |
Returns
A new subclass of the class.
Example
Basic
local Boss = Person:extends("Boss")
Defaults
local Boss = Person:extends("Boss", { title = "The Big Cheese" })
Notes
- Subclasses can use the constructor method.
- You can use extends to subclass a subclass.
- Syntactically this method can be read as "Boss extends Person".
super
Access the super class, which is the class (or subclass) parent. See also Subclasses in the Usage guide.
super
is a property, not a method.
--Call parent methods <className>.super.<classMethod>(self, [args])
Notes
- To call a
super
(parent) method you must use dot (.) syntax, and pass a reference ofself
as the first parameter.
Instances
new
Create a new instance of a class or subclass. See also Instances in the Usage guide.
<className>:new([args])
Arguments
Name | Description | Type | Required |
---|---|---|---|
args | Common delimited set of arguments to pass to the class (or subclass) constructor method, if one exists. See constructor. | Various | N |
Returns
A new instance of the class or subclass.
Example
Basic
local person = Person:new()
Arguments
local person = Person:new("Tina", 23)
Notes
- The passed arguments can be of any Lua type.
Utilities
The following methods are called directly on an object using colon (:) syntax.
instanceOf
A utility method to check if an instance is an instance of a specific class or subclass.
<instance>:instanceOf(<Class>)
Returns
A Boolean value.
Example
local person = Person:new() print(person:instanceOf(Person)) --> true
subclassOf
A utility method to check if a subclass is a subclass of a specific class.
<Subclass>:subclassOf(<Class>)
Returns
A Boolean value.
Example
local Wand = Weapon:extends("Wand") print(Wand:subclassOf(Weapon)) --> true
classOf
A utility method to check if a subclass is derived from a specific class.
<Class>:classOf(<Subclass>)
Returns
A Boolean value.
Example
local Person = Classy.create("Person") local Manager = Person:extends("Manager") print(Person:classOf(Manager)) --> true
Notes
Syntactically this method can be read in reverse as in "is Manager a class of Person".