Lua object-oriented


Object Oriented Programming (OOP) is a very popular computer programming architecture.

The following programming languages ​​support object-oriented programming:


  • C++

  • Java

  • Objective-C

  • Smalltalk

  • C

  • #Ruby


Object-oriented features

  • 1) Encapsulation: refers to the ability to encapsulate the information, functions, and responses of an entity Properties that are all packed into a single object.

  • 2) Inheritance: The inheritance method allows the original program to be expanded without changing it, so that the original functions can be preserved and new functions can be expanded. This helps reduce repeated coding and improve software development efficiency.

  • 3) Polymorphism: The same operation acts on different objects and can have different interpretations and produce different execution results. At runtime, you can call methods in a derived class through a pointer to the base class.

  • 4) Abstraction: Abstraction is a way to simplify complex real-life problems. It can find the most appropriate class definition for specific problems and can explain it at the most appropriate inheritance level. question.


Object-oriented in Lua

We know that objects are composed of properties and methods. The most basic structure in LUA is table, so table needs to be used to describe the properties of objects.

Function in lua can be used to represent methods. Then classes in LUA can be simulated through table + function.

As for inheritance, it can be simulated through metetable (not recommended, only simulating the most basic objects is enough most of the time).


#A table in Lua is not only an object in a certain sense. Like objects, tables also have states (member variables); they also have the nature of being independent from the values ​​of objects. In particular, an object (table) with two different values ​​represents two different objects; an object can also have different values ​​at different times. value, but it is always an object; similar to an object, the life cycle of a table has nothing to do with what it is created from and where it is created. Objects have their member functions, and tables also have:

Account = {balance = 0}
function Account.withdraw (v)
    Account.balance = Account.balance - v
end

This definition creates a new function and saves it in the withdraw field of the Account object. Below we can call it like this:

Account.withdraw(100.00)

A simple Example

The following simple class contains three properties: area, length and breadth. The printArea method is used to print the calculation results:

-- 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

Creating an object is a bit The process of allocating memory for instances of a class. Each class has its own memory and shares common data.

r = Rectangle:new(nil,10,20)

Access properties

We can use the period (.) to access the properties of the class:

print(r.length)

Access member functions

We can use the colon ( :) to access the properties of the class:

r:printArea()

Memory is allocated when the object is initialized.

Complete example

Below we demonstrate a complete example of Lua 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()

Execute the above program, the output result is:

面积为 	100

Lua inheritance

Inheritance means that one object directly uses the properties and methods of another object. Properties and methods that can be used to extend the base class.

The following demonstrates a simple inheritance example:

 -- 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

The following example, 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

Complete example

The following Example: We inherit a simple class to extend the methods of the derived class. The derived class 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()

Execute the above code and the output result is:

面积为 	100
正方形面积为 	100
矩形面积为 	200

Function rewriting

In Lua we can rewrite the functions of the base class and define our own implementation in the derived class:

-- 派生类方法 printArea
function Square:printArea ()
  print("正方形面积 ",self.area)
end