(Lua notes): Lua object-oriented

[Turn] Original link: https://www.runoob.com/lua/lua-object-oriented.html

table of Contents

Lua object-oriented

Typical Case

Create object

Access attributes

Access member functions

Complete example

Lua inheritance

Complete example

Function rewriting


Lua object-oriented

  • Objects are composed of properties and methods .
    • The most basic structure in Lua is table, so table is needed to describe the properties of objects .
    • The function in lua can be used to represent methods.
    • The classes in Lua can be simulated by table + function .
  • As for inheritance, it can be simulated by metatable (not recommended, only the most basic objects are enough for most of the time).
  • A table in Lua is not only an object in a sense.
    • Like objects, tables also have state (member variables)
    • It also has the nature of being independent of the value of the object, especially an object with two different values ​​(table) represents two different objects
    • An object can have different values ​​at different times, but it is always an object
    • Similar to objects, the life cycle of a table has nothing to do with what or where it was 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, which can be called as follows:
Account.withdraw(100.00)

Typical Case

  • The following simple class contains three attributes: area, length and breadth. The printArea method is used to print the calculation results:
-- 元类
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 object

  • Creating an object 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)

Access attributes

  • You can use the dot (.) to access the properties of the class:
print(r.length)

Access member functions

  • You can use the colon: to access the member functions of the class:
r:printArea()
  • Memory is allocated when the object is initialized .

Complete example

  • The following demonstrates a complete example of Lua object-oriented:
-- 元类
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()

输出:面积为     100

Lua inheritance

  • Inheritance means that one object directly uses the properties and methods of another object . 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}
-- 基础类方法 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 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 inherits 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()

输出:
面积为     100
正方形面积为     100
矩形面积为     200

Function rewriting

  • In Lua, you can rewrite the functions of the base class and define your own implementation in the derived class :
-- 派生类方法 printArea
function Square:printArea ()
  print("正方形面积 ",self.area)
end

 

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/108505641