Lua object oriented


Release date:2023-10-10 Update date:2023-10-13 Editor:admin View counts:283

Label:

Lua object oriented

Object-oriented programming is a very popular computer programming architecture.

The following programming languages support object-oriented programming:

  • C++

  • Java

  • Objective-C

  • Smalltalk

  • C#

  • Ruby

Object-oriented feature

  • 1)Encapsulation: a feature that can load the information, functions, and responses of an entity into a single object.

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

  • 3)Polymorphism: the same operation acts on different objects, which can have different interpretations and produce different execution results. At run time, methods in an implementation derived class can be called through apointer to the base class.

  • 4)Abstraction: Abstraction is a way to simplify complex real-world problems, it can find the most appropriate class definition for specific problems, and can 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 the object.

lua in function can be used to represent a method. Then the classesin LUA can be simulated by table + function.

As for inheritance, you can use the metetable simulate it (not recommended, only simulate the most basic objects, most of the implementation is enough).

Table in Lua is not only an object in a sense. Like objects, tables also have states (member variables); they also have the nature of being independent of the values of objects, especially objects with two different values (table) represent two different objects; an object can have differentvalues at different times, but it is always an object; like objects, the life cycle of a table has nothing to do with what is created and where it iscreated. Objects 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 saves it in the Account object’s withdraw domain, we can call the following:

Account.withdraw(100.00)

A simple example

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

Example

-- metaclass
Rectangle = {area = 0, length = 0, breadth = 0}

-- Methods for Derived Classes 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

-- Methods for Derived Classes printArea
function Rectangle:printArea ()
  print("The rectangular area is ",self.area)
end

Create object

Creating an object is the process of allocating memory to an instance of a class. Each class has its own memory and shares common data.

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

Access Properti

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

print(r.length)

Access member function

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

r:printArea()

Allocated when the memory object is initialized.

Complete instance

Here we demonstrate Lua complete example of object-oriented:

Example

-- metaclass
Shape = {area = 0}

-- Basic class methods 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 methods printArea
function Shape:printArea ()
  print("The area is ",self.area)
end

-- create object
myshape = Shape:new(nil,10)

myshape:printArea()

Execute the above program, and the output is as follows:

The area is     100

Lua inheritance

Inheritance means that one object uses the properties and methods of anotherobject directly. Can be used to extend the properties and methods of the underlying class.

The following demonstrates a simple inheritance example:

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

The next example Square object inherits from 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 instance

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

Example

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

-- create object
myshape = Shape:new(nil,10)
myshape:printArea()

Square = Shape:new()
-- Derived class methods new
function Square:new (o,side)
  o = o or Shape:new(o,side)
  setmetatable(o, self)
  self.__index = self
  return o
end

-- Derived class methods printArea
function Square:printArea ()
  print("The square area is",self.area)
end

-- create object
mysquare = Square:new(nil,10)
mysquare:printArea()

Rectangle = Shape:new()
-- Derived class methods 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 methods printArea
function Rectangle:printArea ()
  print("The rectangular area is",self.area)
end

-- create object
myrectangle = Rectangle:new(nil,10,20)
myrectangle:printArea()

Execute the above code, and the output is as follows:

Area of 100
A square area of 100
A rectangular area of 200

Function rewriting

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

-- Derived class methods printArea
function Square:printArea ()
  print("Square area ",self.area)
end

Powered by TorCMS (https://github.com/bukun/TorCMS).