English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Lua Object-Oriented

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

The following programming languages all support object-oriented programming:

  • C++

  • Java

  • Objective-C

  • Smalltalk

  • C#

  • Ruby

Object-Oriented Features

  • 1Encapsulation: It refers to the characteristic of being able to put the information, functions, and responses of an entity into a single object.

  • 2Inheritance: The inheritance method allows for expansion of the program without changing the original program, thus preserving the original function while also expanding the new function. This is beneficial for reducing duplicate coding and improving the efficiency of software development.

  • 3Polymorphism: The same operation applied to different objects can have different interpretations and produce different execution results. At runtime, methods in derived classes can be called through pointers pointing to the base class.

  • 4Abstract: Abstraction is a way to simplify complex real-world problems, it can find the most appropriate class definition for specific problems, and can explain problems at the most appropriate level of inheritance.

Object-Oriented in Lua

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

Functions in Lua can be used to represent methods. So in LUA, classes can be represented by tables + function simulated.

As for inheritance, it can be simulated through metatable (not recommended to use, it is enough to simulate the basic object most of the time).

Tables in Lua are not only objects in a certain sense. Like objects, tables also have state (member variables); they also have an independent nature that is independent of the values of the object (table), as two objects with different values represent two different objects; an object can also have different values at different times, but it is always an object; like objects, the lifecycle of tables is not related to what creates them or where they are created. Objects have their member functions, and tables also have them:

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, and we can call it as follows:

Account.withdraw(100.00)

A simple example

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

-- Metaclass
Rectangle = {area = 0, length = 0, breadth = 0}
-- Derived class method 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
-- Derived class method printArea
function Rectangle:printArea()
  print("Rectangle Area", self.area)
end

Create Object

The process of creating an object is the process of allocating memory for the example of the class. Each class has its own memory and shares common data.

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

Access property

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

print(r.length)

Access member function

We can use the colon : to access the member functions of the class:

r:printArea()

Memory is allocated when the object is initialized.

Complete example

In the following, we demonstrate a complete Lua object-oriented example:

-- Metaclass
Shape = {area = 0}
-- Basic class method 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
-- Basic class method printArea
function Shape:printArea()
  print("Area", self.area)
end
-- Create Object
myshape = Shape:new(nil,10)
myshape:printArea()

After executing the above program, the output result is:

Area     100

Lua Inheritance

Inheritance refers to an object directly using the properties and methods of another object. It can be used to extend the properties and methods of the base class.

The following demonstrates a simple inheritance example:

-- Meta class
Shape = {area = 0}
-- Basic class method 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
-- Basic class method printArea
function Shape:printArea()
  print("Area", self.area)
end

In 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

In the following example, we inherit a simple class to extend the method of the derived class, and the derived class retains the member variables and methods of the inherited class:

-- Meta class
Shape = {area = 0}
-- Basic class method 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
-- Basic class method printArea
function Shape:printArea()
  print("Area", self.area)
end
-- Create Object
myshape = Shape:new(nil,10)
myshape:printArea()
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
-- Derived Class Method printArea
function Square:printArea()
  print("Square Area", self.area)
end
-- Create Object
mysquare = Square:new(nil,10)
mysquare:printArea()
Rectangle = Shape:new()
-- Derived Class Method 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
-- Derived Class Method printArea
function Rectangle:printArea()
  print("Rectangle Area", self.area)
end
-- Create Object
myrectangle = Rectangle:new(nil,10,20)
myrectangle:printArea()

After executing the above code, the output is:

Area     100
Square Area     100
Rectangle Area     200

Function Overriding

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

-- Derived Class Method printArea
function Square:printArea()
  print("Square Area", self.area)
end