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 :: how to enable https service roblox 
Lua :: open while loop lua 
Lua :: lua table of all characters 
Lua :: how to make kill block in roblox lua 
Matlab :: matlab title figure 
Matlab :: matlab title with variable 
Matlab :: log base 10 matlab 
Matlab :: matlab preallocate array size 
Matlab :: octave wait 
Matlab :: pass variable by reference to function in matlab 
Matlab :: BIDS json IntendedFor field examples 
Basic :: excel vba chck that the range is empty 
Basic :: Detailview with form mixing 
Basic :: how to simulate tail in dos/cmd without tail 
Elixir :: generate random number elixir 
Elixir :: elixir append lists 
Elixir :: elixir variables 
Scala :: hashset scala 
Scala :: scala isinstanceof 
Actionscript :: octahedron 
Excel :: excel number of column 
Perl :: perl do while loop 
Perl :: Perl (perl 2018.12) sample 
Pascal :: case of pascal 
Gdscript :: godot get scene root 
Abap :: what is the interface between the abap dictionary and the underlying database management system 
Assembly :: does assembly language use registers 
Assembly :: vba check if object exists 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: jquery unselect option 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =