Lua basic knowledge learning six, object-oriented

1. Object-oriented features

  • 1) Encapsulation: refers to the feature that can pack an entity's information, functions, and responses into a single object.
  • 2) Inheritance: The inherited method allows the original program to be expanded without changing it, so that the original function can be preserved, and the new function can also be expanded. This helps to reduce repetitive coding and improve software development efficiency.
  • 3) Polymorphism: The same operation acts on different objects, which can have different interpretations and produce different execution results. At runtime, methods in the derived class can be called through a pointer 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 explain problems at the most appropriate inheritance level.

2. Object-oriented in Lua

Attributes: use table to describe the attributes of the object.

Method: function can be used to represent a method.

Class: simulated by table + function.

Inheritance: It can be simulated by metetable (not recommended, only simulating the most basic objects is enough for most implementations).

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

A new function is created and stored in the withdraw field of the Account object:

Account.withdraw(100.00)

3. A simple example

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

-- 元类
Rectangle = {area = 0, length = 0, breadth = 0}

-- 派生类的方法 new
-- Rectangle是子类,o是基类
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

4. Create objects

Object creation is the process of allocating memory for an instance of a class. Each class has its own memory and shares common data.

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

5. Access properties

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

print(r.length)

6. Access member functions

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

r:printArea()

Memory is allocated when the object is initialized.

7. Initialization, complete example

-- 元类
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()
Area is 100

8. Lua inheritance

Inheritance is when 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 example of inheritance:

-- Meta class
Shape = {area = 0}
-- 基础类方法 new
function Shape:new (o,side)
  o = o or {}
  setmetatable(o, self)
  --由于返回的是基类o,所以要设定__index,访问基类找不到就找子类
  self.__index = self
  side = side or 0
  self.area = side*side;
  return o
end
-- 基础类方法 printArea
function Shape:printArea ()
  print("面积为 ",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

9. Inheritance, complete example

In the following example, we inherit a simple class to extend the methods of the derived class, and 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, the output is:

area of ​​100 
squares of area 100 
rectangles of area 200

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

Guess you like

Origin blog.csdn.net/u013617851/article/details/124627057