Lua Object-oriented

Source: Internet
Author: User

Introduction of MetaTable:

Lua's table can simulate object-oriented, all thanks to the power of metatable. In a table, if an element of an index is not found, the interpreter goes to the __index element in the metatable under the table to look for in the

However, if you want to modify an element or an assignment in a table, but the element does not exist, the element is created in the table and is not searched for in the metatable.__index.

How the class creation method is implemented:

--function.lua--clone functionfunction Clone (object) Local lookup_table = {} local function _copy (object) if Type (object) ~= "table" then return to Object ElseIf Lookup_table[object] then return lookup_tab Le[object] End Local new_table = {} Lookup_table[object] = new_table for key, value in pairs (o bject) do New_table[_copy (key)] = _copy (value) end return setmetatable (new_table, getmetatable (ob ject)) End return _copy (object) End--class Createclass = function (classname, super) local CLS--inherited fro M Lua Object If super then CLS = Clone (super) Cls.super = Super if not CLS. Ctor then CLS. Ctor = function () end END Else CLS = {Ctor = function () end} end cls.__cname = ClassName Cls.__c Type = 2--Lua Cls.__index = CLS function CLS.        New (...)       Local Instance = setmetatable ({}, CLS) Instance:ctor (...) Return instance end return Clsend 


Two parameters in the 1.class method, classname represents the class name of the class to be created, and super represents the base class of the created class. First class determines if there is a supper, and if there is no base class, an empty construction method is created for this class. If there is a base class, call the Clone method to clone the base class, where the Clone method maps the member functions of the base class to the derived class.

2.New method implementation: Here will be constructed out of the class instance instance of the meta table to the class table itself, and because the class table itself __index parameter is the class table itself. There is no member function in instance, the function is not found when the member function is called with instance, but because the instance inside the Meta table (metatable) is the command class itself, and the class itself also has __index. So also go to the class itself table in the __index search, at this time __index or point to itself, so its member method can be found.

3. The class itself only puts member methods and cannot put member variables for the following reasons:

A. Because of the principle of metatable, that is, only to read the time will go to metatable, when writing if not in instance, will not go to index metatable but add a new in the instance.

B. Because of inheritance, the direct caller of the ctor constructor method is instance, so the variables that are constructed at all levels of the constructor are placed in instance rather than in the class itself table.

The base class implementation of a LUA multi-instance (a class can get objects of multiple classes):

--object.lualocal object = Class ("Object") return object

How to use:
--multi.lualocal object = Require "Object" local multi = Class ("Multi", Object) function Multi:ctor (...)    --super constructor    object.ctor (self)    --class memebers constructor    self.m_mem = 0endfunction multi:show ( )    print (self.m_mem) endlocal Instance1 = Multi:new () instance1:show () Local instance2 = Multi:new () instance2:show ()

The first step: Creating a multi class with the class method inherits from the object class.

The second step: the constructor method in the derived class multi is a reference to the Object.ctor, so the constructor of the derived class multi is to be re-written. Multi class ctor constructs the data of the multi, the first is to construct the data of the parent class in multi, so the ctor of the parent class is called first in the ctor of the derived class. You must use Object.ctor (self) and you cannot use Object:ctor. The principle is this: the invocation origin is the Instance:ctor,object.ctor (self) in the new method is called in the Instance:ctor, so here the self is instance. If written as Object:ctor, then the later Ctor parameter becomes object, so that the data constructed by the parent's construction method is not placed in the instance. In general, because the caller of ctor is instance, the arguments to all ctor methods on the derived chain are instance, which is an instance of the derived class.

Step three: in Super. The ctor call constructs a member variable of the derived class, which is the line Self.m_mem = 0.

Fourth step: Define the method of the multi class show, note that self in all member methods is also instance, because the externally called member method is Instance:func ().

Summary: All member methods in a class, including the Ctor method and the normal method, are called instance, which is the object of the class. Self naturally is also instance rather than multi itself.

The implementation of a LUA single-instance class (that is, one class can initialize only one object):

--singleton.lualocal Object  = Require "Object" local Singleton = Class ("singleton", Object) function Singleton: Getsingleton (...)    if self._instance = = Nil Thenself._instance = self. New (...)    End     return self._instanceend return Singleton    

Analysis: Here Singleton is the highest base class for a single instance, and the Getsingleton method added in this class is essentially used to obtain an instance of the method in all singleton-based methods (the first fetch initializes the instance) instead of the new method. The caller of this method is either the singleton or the derived class itself, so the incoming self is also a singleton or derived class ontology, when you want to add a _instance variable to the class ontology, and call new to get an instance of this variable, which is the only instance.

How to use:
--single.lualocal Singleton = require "Singleton" local single = Class ("single", Singleton) function Single:ctor (...)    --super constructor    singleton.ctor (self)    --class memebers constructor    self.m_mem = 0endfunction Single: Show ()    print (SELF.M_MEM) Endlocal instance = Single:getsingleton () instance:show ()

The overridden constructor method for a derived class is the same, using the Getsingleton method instead of the new method when initializing the instance.

Image parsing memory representations of two base classes:

Lua Object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.