Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Gamedroit.lua - 08/09/2021 - 21:32
- -- Since there is no built-in, let's use a function to create new classes
- -- Parameter n: Function(instance : table)
- function Class(n)
- -- Returns another function that will serve as a constructor, the basis for initializing a new instance.
- -- Parameter ...: table - vararg to receive all parameters
- return function(...)
- -- The table that will store all class data, including methods.
- local data = {}
- -- Here we create a Metatable that will generate the expected behavior of a class system
- local instance = setmetatable({}, {
- -- Metamethod fired every time a new variable is added to the table.
- -- Note that as its first argument, setmetatable takes an empty table,
- -- so every time the programmer changes the value of a variable this function will be called
- -- because the values are being written to "data" table.
- __newindex = function(_, index, value)
- -- Variables with the "__" prefix are defined as private variables, only accessible inside classes,
- -- the expression below takes care of checking if the variable is private or not,
- -- besides checking if it is being accessed inside the class or outside.
- -- Note: main refers to scope outside the class.
- if (index:sub(1, 2) == "__" and debug.getinfo(2).what == "main") then
- return nil
- end
- -- If everything is ok, save changes in data table.
- data[index] = value
- end,
- -- Metamethod fired every time a table index is accessed.
- __index = function(_, index)
- if (index:sub(1, 2) == "__" and debug.getinfo(2).what == "main") then
- return nil
- end
- -- If everything is ok, returns the index value within the data table.
- return data[index]
- end
- })
- -- Call our constructor to define our instance's default variables.
- n(instance)
- -- Fires the "main" function with the arguments passed to the constructor.
- instance:main(...)
- -- returns the instance of the class.
- return instance
- end
- end
- -- Here a simple test, we create the Person class that defines a global variable name
- -- and a private variable __createdAt that refers to the creation date.
- local Person = Class (function(self)
- self.__createdAt = os.time()
- self.name = ""
- -- The main function, which will always be called at the beginning of a new instance,
- -- which receives the parameter "name".
- function self:main(name)
- -- Sets the value of name on instance to the value of the parameter.
- self.name = name
- end
- -- A function that serves to return the value of the private variable __createdAt
- function self:getCreatedAt()
- return self.__createdAt
- end
- function self:setCreatedAt(value)
- self.__createdAt = value
- end
- end)
- -- Instance the Person class with the name "Andrew".
- local person1 = Person("Andrew")
- -- Attempts to access the __createdAt private variable from outside the class
- -- and tries to set a value to it. It doesn't return an error but it doesn't make any changes.
- person1.__createdAt = 12
- -- Prints the person's name -> 'Andrew'.
- print('name:', person1.name)
- -- Trying to access the value of the __createdAt variable directly. -> 'nil'
- print('direct:', person1.__createdAt)
- -- Trying to use getter method for __createdAt. -> 'time : number'
- print('getter:', person1:getCreatedAt())
- -- Trying to use the setter method for __createdAt
- person1:setCreatedAt(100)
- -- getter method for __createdAt. -> '100'
- print('getter:', person1:getCreatedAt())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement