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 :: change material roblox lua 
Lua :: lua table unpack 
Lua :: break in lua 
Lua :: roblox lua wait for player to load 
Lua :: print a table in lua 
Lua :: get index of value in table lua 
Lua :: hello world in lua 
Lua :: lua push to aray 
Lua :: free roblux 
Lua :: lua split 
Lua :: attempt to call a string value lua 
Lua :: unsur unsur hidrogen 
Lua :: What percentage of developers use Lua 
Lua :: lagstep roblox 
Lua :: Simple Roblox Lua Function 
Matlab :: matlab how to get object type 
Matlab :: matlab 
Matlab :: odd even in array matlab 
Basic :: cmd cant change directory 
Basic :: NÃO CONSIGO MANDAR UM POST EM VBNET USANDO POSTMAN 
Elixir :: elixir try rescue 
Elixir :: liveview component mount 
Scala :: scala hello world 
Scala :: add method to string class scala 
Actionscript :: truncate restart identity - syntax error at or near "identity" 
Excel :: excel paste across cells 
Perl :: perl sigils 
Pascal :: delay() in Pascal 
Powershell :: Auto-open DevTools on every new tab For powershell on Windows 
Abap :: abap alv popup 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =