May 12, 2021 Lua
Object Oriented Programming (OOP) is a very popular computer programming architecture.
There are several programming languages that support object-oriented programming:
We know that objects are made up of properties and methods. The most basic structure in LUA is table, so you need to use table to describe the properties of an object.
Fusion in lua can be used to represent methods. Then the classes in the LUA can be simulated by table and function.
As for inheritance, it can be simulated by metalable (not recommended, only the most basic objects are used most of the time).
The table in Lua is not only an object in a sense. L ike an object, a table has states (member variables), and there is a nature that is independent of the value of an object, especially if the table, which has two different values, represents two different objects; an object can have different values at different times, but it is always an object; and like an object, the life cycle of a table has nothing to do with what it was created by and where it was created. O bjects have their member functions, and tables have:
Account = {balance = 0} function Account.withdraw (v) Account.balance = Account.balance - v end
This definition creates a new function and is saved in the withdraw domain of the Count object, which we can call here:
Account.withdraw(100.00)
The following simple class contains three properties: area, length, and breadth, and the printArea method is used to print the results of the calculation:
-- Meta class Rectangle = {area = 0, length = 0, breadth = 0} -- 派生类的方法 new function Rectangle:new (o,length,breadth) o = o or {} setmetatable(o, self) self.__index = self self.length = length or 0 self.breadth = breadth or 0 self.area = length*breadth; return o end -- 派生类的方法 printArea function Rectangle:printArea () print("矩形面积为 ",self.area) end
Creating an object is the process by which an instance of a bit class allocates memory. Each class has its own memory and shares public data.
r = Rectangle:new(nil,10,20)
We can use the dot (.) to access the properties of the class:
print(r.length)
We can use a colon (:) to access the properties of the class:
r:printArea()
Memory is allocated when the object is initialized.
Here's a full example of Lua's object-oriented:
-- Meta class Shape = {area = 0} -- 基础类方法 new function Shape:new (o,side) o = o or {} setmetatable(o, self) self.__index = self side = side or 0 self.area = side*side; return o end -- 基础类方法 printArea function Shape:printArea () print("面积为 ",self.area) end -- 创建对象 myshape = Shape:new(nil,10) myshape:printArea()
The above procedure is performed and the output is:
面积为 100
Inheritance refers to the properties and methods of one object that use another directly. Properties and methods that can be used to extend the underlying class.
Here's a simple example of inheritance:
-- Meta class Shape = {area = 0} -- 基础类方法 new function Shape:new (o,side) o = o or {} setmetatable(o, self) self.__index = self side = side or 0 self.area = side*side; return o end -- 基础类方法 printArea function Shape:printArea () print("面积为 ",self.area) end
In the next instance, the Square object inherits the Shape class:
Square = Shape:new() -- Derived class method new function Square:new (o,side) o = o or Shape:new(o,side) setmetatable(o, self) self.__index = self return o end
The following example we inherited a simple class to extend the method of deriving a class, which retains the member variables and methods of the inherited class:
-- Meta class Shape = {area = 0} -- 基础类方法 new function Shape:new (o,side) o = o or {} setmetatable(o, self) self.__index = self side = side or 0 self.area = side*side; return o end -- 基础类方法 printArea function Shape:printArea () print("面积为 ",self.area) end -- 创建对象 myshape = Shape:new(nil,10) myshape:printArea() Square = Shape:new() -- 派生类方法 new function Square:new (o,side) o = o or Shape:new(o,side) setmetatable(o, self) self.__index = self return o end -- 派生类方法 printArea function Square:printArea () print("正方形面积为 ",self.area) end -- 创建对象 mysquare = Square:new(nil,10) mysquare:printArea() Rectangle = Shape:new() -- 派生类方法 new function Rectangle:new (o,length,breadth) o = o or Shape:new(o) setmetatable(o, self) self.__index = self self.area = length * breadth return o end -- 派生类方法 printArea function Rectangle:printArea () print("矩形面积为 ",self.area) end -- 创建对象 myrectangle = Rectangle:new(nil,10,20) myrectangle:printArea()
The above code is executed and the output is:
面积为 100 正方形面积为 100 矩形面积为 200
In Lua we can override the functions of the underlying class and define our own implementation in the derived class:
-- 派生类方法 printArea function Square:printArea () print("正方形面积 ",self.area) end