(reproduced in this article) the use of learning, infringement of the deletion!
Original Address Http://blog.csdn.net/y_23k_bug/article/details/19965877?utm_source=tuicool&utm_medium=referral
Lua Object-oriented implementations:
A class is like a stencil that creates an object. Some object-oriented languages provide the concept of classes in which each object is an instance of a particular class. LUA does not have a class concept, and each object can only customize behavior and patterns. However, it is not difficult to emulate classes in Lua.
In Lua, object-oriented is implemented using the meta-table mechanism.
First, in general, a table and its meta-tables are different individuals (not the same table), and the meta-table is not created automatically when a new table is created.
setmetatable function: Setting the meta-table
Setmetatable (table 1, table 2) Hooks table 2 as a meta-table of table 1 and returns the linked table 1
__index field:
The __index field in the Meta-table is a very powerful field that provides support for backtracking queries. and object-oriented implementation is based on backtracking query
When accessing a field that does not exist in a table, the result is nil. But if the table had a meta-table, the access would look for the __index field in the Meta-table. If no __index field is assigned, then the access result is nil. Otherwise, the final result is provided by the __index field.
__index can be assigned to a function or to a table. is a function, this function is called. is a table, the table is re-accessed in the same way.
Note: There are three tables of individuals,
The table we directly manipulate, called Table A, table A's metatable called Table B, table B's __index field assignment table, called Table C
If you access a field in table A, if you do not find it, you will see if Table A has no meta table B, if any, it will look for the __index field in B has an assignment, if the assignment is Table C, it will go to table C to find the field you want to access, if found, return that field, if not, Returns NIL.
Lua object-oriented-object creation
We can use the meta-table and meta-table __index fields to implement the creation of class objects
In the constructor for the class, define a new table and set the Class (table) to the __index field of the newly defined table's meta-table, so that when we invoke a field of that class with an object of the instance, we go to the class to find the call, thus implementing the instantiation of the object.
Example
Class.lua
Local class = {}
function Class:new ()
Local Self = {}-Creates a new table as an instance of an object
Setmetatable (self, {__index = class})-set class as the Object meta-table __index
Return self-returns the new table
End
function Class:func ()
Print ("Class:func")
End
Return class
Main.lua
Local class = require ("class")
S1 = class:new ()-Instantiates an object S1
S1:func () ——->class:func
Lua object-oriented-inheritance
LUA implements inheritance and implements object instantiation in the same way, using the __index field of a meta-table and a meta-table.
Example
Class1.lua
Local Class1 = {}
function Class1:func1 ()
Print ("Class1:func1")
End
return Class1
Class2.lua
Local Class2 = {}
Local Class1 = require ("Class1")
function Class2:func2 ()
Print ("Class2:func2")
End
function Class2:new ()
Setmetatable (Class2, {__index = class1})-Set Class1 field Class2 for __index to implement inheritance
-Instance Object
Local self = {}
Setmetatable (self, {__index = Class2})
return self
End
return Class2
Main.lua
Local Class2 = require ("Class2")
Local S1 = Class2:new ()
S1:FUNC1 () ——— >class1:func1
S1:FUNC2 () ——— >CLASS2:FUNC2
Lua-oriented object-polymorphic
LUA supports polymorphic
Example:
Class1.lua
Local Class1 = {x = 0,y = 0}
function class1:new (x, y)
-Body
Local self = {}
Setmetatable (SELF,CLASS1)
Class1.__index = Class1
self.x = X
Self.y = y
return self
End
function Class1:test ()
Print (SELF.X,SELF.Y)
End
function Class1:gto ()
Return 100
End
function Class1:gio ()
Return Self:gto ()
End
return Class1
Class2.lua
Local Class2 ={}
Local Class2 = require ("Class1")
function class2:new (x, y)
Setmetatable (Class2, Class1)
Class1.__index = Class2
Local self = {}
Setmetatable (self, class2)
Class2.__index = Class2
self.x = X
Self.y = y
return self
End
function Class2:gto ()
Return 50
End
return Class2
Main.lua
Class1 = require ("Class1")
Class2 = require ("Class2")
S1 = class1:new ()
S2 = class2:new ()
Print (S1:gio ()) ——->200
Print (S2:gio ())-->100
The-s2 object calls the Gio function of the base class Class1, which calls the Class2 GTO function inside the function to achieve polymorphism.
Lua Object-oriented-Multiple inheritance
The multi-inheritance implementation of a class in Lua is also a __index field that leverages the metatable and meta tables, unlike object instantiation and single inheritance, where the __index field is assigned a function rather than a base class table.
Use the function that passed in the __index field to find a field that is not found in the class (a function that traverses multiple base classes that inherit from the class)
Lookup function:
Local function search (k,plist)
For i = 1, #plist do
Local v = plist[i][k]
If v then return v end
End
End
-plist is a collection of base classes for this class, and K is the field to find (Call an inherited field)
To implement an inheritance function:
Local function Createclass ()
Local parents = {Class1,class2}
Setmetatable (Class3,{__index = function (t,k)
Return Search (k,parents)
End})
End
This allows for multiple inheritance.
Example:
Class1.lua
Local Class1 = {}
function Class1:func1 ()
Print ("Class1--func1")
End
return Class1
Class2.lua
Local Class2 ={}
function Class2:func2 ()
Print ("Class2:func2")
End
return Class2
Class3.lua
Local CLASS3 = {}
Local Class1 = require ("Class1")
Local Class2 = require ("Class2")
Local function search (k,plist)
For i = 1, #plist do
Local v = plist[i][k]
If v then return v end
End
End
Local function Createclass ()
Local parents = {Class1,class2}
Setmetatable (Class3,{__index = function (t,k)
Return Search (k,parents)
End})
End
function class3:func3 ()
Print ("class3:func3")
End
function Class3:new ()
Local self = {}
Createclass ()
Setmetatable (SELF,CLASS3)
Class3.__index = Class3
return self
End
Return CLASS3
Main.lua
Local CLASS3 = require ("CLASS3")
Local S1 = Class3:new ()
S1:FUNC1 () ————->class1:func1
S1:FUNC2 () ———— >CLASS2:FUNC2
S1:FUNC3 () ———— >class3:func3
Lua object-oriented--singleton mode
Lua's singleton pattern is implemented using a global table.
Example:
Catmanager = {}
CATMANAGER_MT = {__index = catmanager}-create a table to do the instance object's meta-table, __index set to this singleton class
function Catmanager:new ()
Local self = {}
Setmetatable (self, CATMANAGER_MT)-__index field that sets the global table Catmanager to the meta table of the self (newly created table)
-Each time a singleton is obtained, a self table (object) is created that inherits the global table Catmanager, and each time the field in the global table is modified, the field is modified the next time it is called again.
return self
End
function Catmanager:func1 ()
Print ("Func1")
End
Main.lua
Require ("Catmanager")
Catmanager = Catmanager:new ()
Once imported, the entire program can be used to achieve a single case effect
Shuyujingerfengbuzhi, son want to raise and pro not!
December 19, 2016 17:23:18
Lua Object-oriented----classes, inheritance, multi-inheritance, and Singleton implementations