Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua is object-oriented


May 12, 2021 Lua


Table of contents


Lua is object-oriented

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

There are several programming languages that support object-oriented programming:

  • C++
  • Java
  • Objective-C
  • Smalltalk
  • C #
  • Ruby

Object-oriented features

  • 1) Encapsulation: A feature that can load information, functions, and responses of an entity into a single object.
  • 2) Inheritance: The inherited method allows it to be expanded without changing the original program, allowing the original functionality to be saved and the new functionality to be extended. This is conducive to reducing duplicate coding and improving the efficiency of software development.
  • 3) Polymorphism: The same operation is performed on different objects and can be interpreted differently, resulting in different execution results. At runtime, you can call methods in the implementation derived class by pointing to the base class.
  • 4) Abstraction is a way to simplify complex real-world problems, to find the most appropriate class definition for a specific problem, and to explain the problem at the most appropriate inheritance level.

Object-oriented in Lua

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)

A simple example

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

Create an object

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)

Access the property

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

print(r.length)

Access member functions

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

r:printArea()

Memory is allocated when the object is initialized.

The full instance

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

Lua inherits

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

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

Function override

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