Simple lua class impl in one lua file.
-
Download
LuaClass.lua
file and put in your project. -
Import
LuaClass
in front of your code.require("LuaClass")
-
Use function
YourClassName = class(baseClass, attrsDict)
to create a new class. Then define class member methods usefunction YourClassName:memberMethod() end
.Vec3 = class(nil, { x = 0, y = 0, z = 0 }) function Vec3:length() return math.sqrt((self.x * self.x) + (self.y * self.y) + (self.z * self.z)) end
-
Use function
YourClassName:new(attrsDict)
to create a class instance.vec3 = Vec3:new({x = 1, y = 2, z = 3}) print(vec3:length())