Search
 
SCRIPT & CODE EXAMPLE
 

LUA

lua class

MyClass = {foo = "Hello", x = 0} -- Default values
MyClass.__index = MyClass

function MyClass.new(arg)
    local object = {}
    setmetatable(object, SomeClass)
  
  	object.arg = arg
  
    return object
end

function MyClass:bar() -- Be careful, not a '.' but a ':'
  print("World !")
end

function MyClass:addOne()
  self.x = self.x + 1
end

obj1 = MyClass.new("a")
obj2 = MyClass.new("b")

print(obj1.foo) -- Print : Hello
obj2.bar() -- Print : World !

print(ob1.x) -- Print : 0
print(ob2.x) -- Print : 0
obj2:addOne()
print(ob1.x) -- Print : 0
print(ob2.x) -- Print : 1

print(obj1.arg) -- Print : a
print(obj2.arg) -- Print : b
Comment

lua object

SomeClass = { id = "some_class" }
SomeClass.__index = SomeClass
function SomeClass:Create()
    local this = {}
    setmetatable(this, SomeClass)
    return this
end

c1 = SomeClass:Create()
c2 = SomeClass:Create()

print(c1.id) -- "some_class"
print(c2.id) -- "some_class"
Comment

lua class

-- Classes in lua are just tables.
className = {}

--[[
	className:methodName(parameters) is the intended way to make
	a constructor or a method for the class.
    
    className.methodName(parameters) however,
    is preferred to make static methods.
--]]
function className:new(...)
	self.__index = self
    return setmetatable({...}, self)
end

function className:isInstance(instance)
	return getmetatable(instance) == self
end

c = className:new(1, 2, 3)
print(className:isInstance(c)) -- returns true
Comment

lua class example

Car = {}

function Car.new(position, driver, model)
    local newcar = {}

    newcar.Position = position
    newcar.Driver = driver
    newcar.Model = model

    return newcar
end
Comment

PREVIOUS NEXT
Code Example
Lua :: roblox player chatter event 
Lua :: lua print table 
Lua :: cmder not taking lua file 
Lua :: while loop in lua 
Lua :: How to Register a command in Lua 
Lua :: roblox add attribute 
Lua :: how to make a day/night script roblox 
Lua :: random number lua 
Lua :: lua patterns 
Lua :: lua coding lines to test with 
Lua :: how to check if table is clear 
Lua :: open while loop lua 
Matlab :: matlab title figure 
Matlab :: matlab number to string 
Matlab :: display sequence in matlab 
Matlab :: sin in scilab 
Basic :: add user to multiple groups ubuntu 
Basic :: xolo themeforest 
Elixir :: elixir string to date 
Elixir :: elixir enum all 
Elixir :: elixir variables 
Scala :: How to make immutable variable in scala 
Scala :: map function scala 
Excel :: google sheets select item from split 
Excel :: google sheets filter cells that match word 
Perl :: perl mechanize infinite scroll with attempt count 
Pascal :: does not equal in pascal 
Gdscript :: godot ignore function 
Lisp :: Doom emacs pdf-tools 
Assembly :: how to import servo library in arduino 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =