-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaTable.lua
210 lines (193 loc) · 5.55 KB
/
MetaTable.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by KipKips.
--- DateTime: 2020.3.16 22:28
---
---设置普通表和元表的关联
print()
print("---------设置普通表和元表的关联-------------------------")
print()
tab={"Lua","Java","C#","C++"} --普通表
metatab={}--元表
setmetatable(tab,metatab) --将metatab设置为tab的元表,会将普通表返回,元表扩展了普通表的行为
st=setmetatable(tab,metatab)--普通表
print(st[1],st[2],st[3],st[4])
gt=getmetatable(tab)--获取元表
print(gt,metatab)
--简要写法
metatab={}
tab=setmetatable({"asd","asda","h"},metatab)
print()
print("--------metatable的用法-------------------------")
print()
---__metatable的用法
metatab={__metatable="assdgd",1,2}
tab=setmetatable({"asd","asda","h"},metatab)
print(tab[1])
--元表的__metatable存在值
print(getmetatable(tab)[1]) --无法访问
print(getmetatable(tab))--返回__metatable键的对应值
print()
print("--------index用法(指向函数)-------------------------")
---__index用法(指向函数)
print()
--元表中的键是有限制的,不能随意写
metatab={
__metatable="assdgd",
--__index为一个函数,参数是固定的,参数tab为关联的普通表,key为访问索引
--__index指向的函数可以有返回值
__index=function(tab,key)
--print("不存在索引"..key.."的值")
return "不存在索引"..key.."的值"
end
}
tab=setmetatable({"Lua","C#","C++","Java","Python"},metatab)
print(tab[1])--若索引可以正常访问,则不调用元表的__index指向的函数
print(tab[10])--若索引不可以访问,则调用__index指向的函数,有返回值,则输出返回值
a=tab[10]
print(a)
print()
print("----------index的用法(指向表)-------------------------")
---__index的用法(指向表)
print()
indextab={}
indextab[5]="Nodejs"
indextab[6]="Golang"
indextab[7]="PHP"
indextab[8]="JavaScript"
indextab[9]="SQL"
indextab[10]="Swift"
metatab={
__metatable="lock",
--__index为一个表,若索引不可以访问,则在__index的表中查询,若索引可以正常访问,则正常返回
__index=indextab
}
tab=setmetatable({"Lua","C#","C++","Java","Python"},metatab)
print(tab[10])
print(tab[5])--若索引在tab和__index指向的表中都可以访问,优先访问tab的索引值
print()
print("--------newindex的用法(指向函数)-------------------------")
print()
---newindex的用法(指向函数)
---修改新的索引才会起作用(添加新的数据),并且不进行赋值操作
tab={"Lua","C#","C++","Java","Python"}
metatab={
__newindex=function(tab,key,value)--tab为操作的表,key为操作的索引,value为改变的值
print("tab的索引"..key.."的值为"..value)
rawset(tab, key,value)---rawset方法将表中值更新
end
}
setmetatable(tab,metatab)
tab[6]="C"--调用__newindex指向的函数,不进行赋值操作,除非进行rawset(tab,key,value)操作
print(tab[6])
tab[5]="C"--无输出
print(tab[5])
---newindex的用法(指向表)
print()
print("--------newindex的用法(指向表)-------------------------")
print()
tab={"Lua","C#","C++","Java","Python"}
newindextab={}
metatab={
__newindex=newindextab
}
setmetatable(tab,metatab)
tab[5]="Rust" --操作的索引在tab中,对tab进行操作
print(tab[5])
tab[6]="C" --若操作的索引不在tab中,将数据添加到__newindex指向的表中去,索引为该索引
print(tab[6])
print(newindextab[6])
print()
print("----------为表添加操作符-------------------------")
print()
---为表添加操作符
tab={"Lua","C#","C++","Java","Python"}
newtab={"PHP","C","Rust","SQL"}
metatab={
--__add值指向函数,参数为当前表和新表
__add=function(tab,newtab)
---第一种插入方法
--for i=1,#newtab do
-- table.insert(tab,newtab[i])
--end
---第二种插入方法
for i=1,#newtab do
tab[1+#tab]=newtab[i]
end
return tab
end,
__concat=function(tab,str)
for i=1,#tab do
tab[i]=tab[i]..str
end
return tab
end,
--__tostring用来定义print(tab)的输出
__tostring = function(tab)
sum = ""
for k in pairs(tab) do
sum = sum..tab[k].." "
end
return sum
end,
--__unm取负数
__unm=function(tab)
for k in pairs(tab) do
tab[k]="-"..tab[k]
end
return tab
end
}
setmetatable(tab,metatab)
tab=tab+newtab
for i = 1, #tab do
print(tab[i])
end
tab=tab.."_wkp"
for i = 1, #tab do
print(tab[i])
end
print(tab)
tab=-tab
for i = 1, #tab do
print(tab[i])
end
print()
print("------------call方法-----------------------------------")
print()
tab={"Lua",1,"C#","C++",2,3,"Java",4,"Python"}
newtab={"PHP","C","Rust","SQL"}
metatab= {
__add=function(tab,newtab)
---第一种插入方法
--for i=1,#newtab do
-- table.insert(tab,newtab[i])
--end
---第二种插入方法
for i=1,#newtab do
tab[1+#tab]=newtab[i]
end
return tab
end,
--当表被当做函数来调用时,会使用__call
__call=function(tab,arg1,arg2)
if type(arg1)=="number" then
for i = 1, #tab do
if type(tab[i])=="number" then
tab[i]=tab[i]+arg1+arg2
end
end
else
for i = 1, #tab do
if type(tab[i])=="string" then
tab[i]=tab[i].." "..arg1.." "..arg2
end
end
end
end
}
setmetatable(tab,metatab)
tab("1","1")
for i = 1, #tab do
print(tab[i])
end