table of Contents
Setting and obtaining of meta table:
The lookup relationship between table and meta table:
1. Blog introduction
This article, as a Lua study note, talks about the related content of Lua meta table. The main content includes what is meta table, how to use meta table and the purpose of some meta methods. The knowledge points are excerpted from the rookie tutorial, and readers can directly Jump to the relevant content of the Lua metatable in the rookie tutorial. There is a portal at the bottom of the blog post.
2. Content
What is a meta table:
A meta table is a table, this table has default meta method and custom method attributes, and can be used as a description of another table, this table can be called a meta table
The role of the meta table:
mytable = {}
mymetatable = {}
When you set the table mymetatable as the meta table of the mytable table, mytable can enjoy all the properties and methods of mymetatable, like inheritance. When you call the properties or methods in mytable, if you can't find it, you will go to its meta table. Search again within the table, if some arithmetic operations are performed on the table, it will trigger some related default meta methods in the meta table
Setting and obtaining of meta table:
mytable = {} -- 普通表
mymetatable = {} -- 元表
setmetatable(mytable,mymetatable) -- 把 mymetatable 设为 mytable 的元表
getmetatable(mytable) -- 这回返回mymetatable
mytable = setmetatable({},{})
We can set a table as the meta table of another table through setmetatable. This method returns a table with mymetatable as the meta table, so it can be abbreviated directly into the form of mytable = setmetatable({},{}), we can also pass getmetatable to get the meta table of a table, this meta table can be nil.
The lookup relationship between table and meta table:
mytable = {}
mymetatable = {}
setmetatable(mytable,mymetatable)
mymetatable.__index = mymetatable
mymetatable.key2 = 100
print(mytable.key2)
--------------------------输出
100
The meta table has some default meta methods and some attributes. These method attributes all start with two lower bars (__). Here, the most important default attribute of a meta table is __index. __index can be set to any data type. Either method or table can be used. Here we need to emphasize the search order after calling the attribute. As shown in the above code, if we call an attribute key2 in mytable, we will find that there is no key2 attribute in mytable, and then we will Go to mytable's metatable mymetatable to find key2. In fact, it is not directly in mymetatable to find key2, but in the attribute __index of mymetatable, so in the above I set __index to mymetatable itself, so that it can be found attribute key2 in mymetatable
Search order:
1. First find whether there is key2 in mytable, and return if it exists
2. If key2 does not exist in mytable, first look for whether there is a metatable in mytable, and end if it does not exist.
3. There is a metatable mymetatable in mytable, search for key2 in __index in mymetatable, return if it exists, and end if it does not exist
Purpose of meta-methods:
local mytable1 = {1,2,3}
local mytable2 = {4,5,6}
print(mytable1 + mytable2)
As shown in the above code, under normal circumstances, we cannot directly perform addition, subtraction, multiplication, and division operations on the two tables, and an error will be reported, but if there is a metatable, some metamethods in the metatable will be triggered when the operator is encountered , For example, the + method will trigger the __add method in the meta table.
local mytable1 = {1,2,3}
local mytable2 = {4,5,6}
mymetatable = {}
setmetatable(mytable2,mymetatable)
function mymetatable:__add(table1,table2)
return 1
end
print(mytable1 + mytable2)
-------------------------输出
1
As shown in the above code, if there is an addition behavior of tables and tables, it will judge whether the table has a meta table in the order of addition. If there is a meta table, whether there is a meta method __add in the meta table, and then call the first A found __add method, the __add method has a return value, if there is no metatable or there is no __add method in the metatable, the compilation fails. The meta-methods related to calculations are listed below, and some Readers of non-operation-related meta methods can refer to the rookie tutorial.
__add | The corresponding operator'+'. |
__sub | The corresponding operator'-'. |
__I have | The corresponding operator'*'. |
__div | The corresponding operator'/'. |
__mod | The corresponding operator'%'. |
__unm | The corresponding operator'-'. |
__concat | The corresponding operator'...'. |
__eq | The corresponding operator'=='. |
__lt | The corresponding operator'<'. |
__the | The corresponding operator'<='. |
3. Push
Github:https://github.com/KingSun5
Novice tutorial Lua meta table: https://www.runoob.com/lua/lua-metatables.html
4. Conclusion
Meta-tables have a wide range of functions. One of them is to implement inheritance as a simulation object-oriented. This blog is about here. If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger, like the blog post, and blog another. The main ability is limited. If there are any mistakes in the article, you are welcome to comment and criticize.
QQ exchange group: 806091680 (Chinar)
This group was created by CSDN blogger Chinar, recommend it! I am also in the group!