File tree 6 files changed +141
-1
lines changed
6 files changed +141
-1
lines changed Original file line number Diff line number Diff line change
1
+ --
2
+ -- Created by IntelliJ IDEA.
3
+ -- User: Administrator
4
+ -- Date: 2017/5/23
5
+ -- Time: 16:51
6
+ -- To change this template use File | Settings | File Templates.
7
+ -- [1]
8
+ -- local other = { foo = 3 }
9
+ -- t = setmetatable({}, { __index = other })
10
+ -- print(t.foo) -- 3
11
+ -- print(t.far) -- nil
12
+
13
+ -- [2] 如果__index包含一个函数的话,Lua就会调用那个函数,table和键会作为参数传递给函数
14
+ -- mytable = setmetatable({key1 = "value1"}, {
15
+ -- __index = function(mytable, key)
16
+ -- if key == "key2" then
17
+ -- return "metatablevalue"
18
+ -- else
19
+ -- return nil
20
+ -- end
21
+ -- end
22
+ -- })
23
+ --
24
+ -- print(mytable.key1,mytable.key2)
25
+ mytable = setmetatable ({key1 = " value1" }, { __index = { key2 = " metatablevalue123" } })
26
+ print (mytable .key1 ,mytable .key2 ) -- value1 metatablevalue123
27
+
28
+
Original file line number Diff line number Diff line change
1
+ --
2
+ -- Created by IntelliJ IDEA.
3
+ -- User: Administrator
4
+ -- Date: 2017/5/23
5
+ -- Time: 16:57
6
+ -- To change this template use File | Settings | File Templates.
7
+
8
+ -- __newindex 元方法用来对表更新,__index则用来对表访问
9
+ -- 当你给表的一个缺少的索引赋值,解释器就会查找__newindex 元方法:如果存在则调用这个函数而不进行赋值操作
10
+ -- mymetatable = {}
11
+ -- mytable = setmetatable({key1 = "Value1"}, {__newindex = mymetatable})
12
+ --
13
+ -- print(mytable.key1) -- Value1
14
+ --
15
+ -- mytable.newkey = 'New Value2'
16
+ -- print(mytable.newkey,mymetatable.newkey) -- nil New Value2
17
+ --
18
+ -- mytable.key1 = "新值1"
19
+ -- print(mytable.key1,mymetatable.key1) -- 新值1 nil
20
+
21
+ -- 以上实例中表设置了元方法 __newindex
22
+ -- [1] 在对新索引键(newkey)赋值时(mytable.newkey = "新值2"),会调用元方法,而不进行赋值。
23
+ -- [2] 而如果对已存在的索引键(key1),则会进行赋值,而不调用元方法 __newindex
24
+
25
+ -- 以下实例使用了 rawset 函数来更新表
26
+ mytable = setmetatable ({key1 = " value1" }, { __newindex =
27
+ function (mytable , key , value )
28
+ rawset (mytable , key , " \" " .. value .. " \" " )
29
+ end
30
+ })
31
+
32
+ mytable .key1 = " new value"
33
+ mytable .key2 = 4
34
+
35
+ print (mytable .key1 ,mytable .key2 )
Original file line number Diff line number Diff line change
1
+ --
2
+ -- Created by IntelliJ IDEA.
3
+ -- User: Administrator
4
+ -- Date: 2017/5/23
5
+ -- Time: 17:12
6
+ -- To change this template use File | Settings | File Templates.
7
+ -- 那么LUA中的类可以通过table + function模拟出来
8
+
9
+ -- Meta class
10
+ Shape = { area = 0 }
11
+
12
+ -- 基础类方法 new
13
+ function Shape :new (o , side )
14
+ o = o or {}
15
+ setmetatable (o , self ) -- self其实就相当于Java,C++中的this对象
16
+ self .__index = self
17
+ side = side or 0
18
+ self .area = side * side ;
19
+ return o
20
+ end
21
+
22
+ -- 基础类方法 printArea
23
+ function Shape :printArea ()
24
+ print (" 面积为 " , self .area )
25
+ end
26
+
27
+ -- 创建对象
28
+ myshape = Shape :new (nil , 10 )
29
+
30
+ -- 对象实例去访问该类的方法
31
+ myshape :printArea ()
32
+
33
+ -- setmetatable(table,metatable): 对指定table设置元表(metatable)
Original file line number Diff line number Diff line change
1
+ --
2
+ -- Created by IntelliJ IDEA.
3
+ -- User: Administrator
4
+ -- Date: 2017/5/23
5
+ -- Time: 17:29
6
+ -- To change this template use File | Settings | File Templates.
7
+ --
8
+ -- local a = {} -- 普通表
9
+ -- local b = {k = 11} -- 元表
10
+ -- setmetatable(a,b) -- 把 b 设为 a 的元表
11
+ -- print(a.k) -- nil
12
+ -- b.__index = b --设置元方法
13
+ -- b.v = 22 --给b表增加一个属性
14
+ -- a.aa = 33 --给a表增加一个属性
15
+ -- print(a.k) --11,因为a有元表b,且存在元方法,可以索引到b表中对应的值
16
+ -- print(a.v) --22,设定关系后,b增加的属性,a也可以索引到
17
+ -- print(b.aa) --nil,相反,b并不可以索引到a的值
18
+
19
+ local a = {} -- 普通表
20
+ local b = {k = 11 } -- 元表
21
+ setmetatable (a ,{__index = b } )
22
+ print (a .k )
23
+ b .v = 1020
24
+ a .aa = 3030
25
+ print (a .v ) -- 22,设定关系后,b增加的属性,a也可以索引到
26
+ print (a .aa ) -- nil,相反,b并不可以索引到a的值
27
+
Original file line number Diff line number Diff line change
1
+ ## lua中self.__ index = self是什么意思?
2
+ + 设置原表语法:` setmetatable(table,metatable): 对指定table设置元表(metatable) `
3
+ + [ http://www.cnblogs.com/mentalidade/p/6561418.html ] ( http://www.cnblogs.com/mentalidade/p/6561418.html )
4
+ + 当一个表存在元表的时候可能会发生改变。lua当发现一个不存在的key时,如果这个table存在元表,则会尝试在元表中寻找是否有匹配key对应的value
5
+ ``` lua
6
+ local a = {} -- 普通表
7
+ local b = {k = 11 } -- 元表
8
+ setmetatable (a ,b ) -- 把 b 设为 a 的元表
9
+ b .__index = b -- 设置元方法
10
+ b .v = 22 -- 给b表增加一个属性
11
+ a .aa = 33 -- 给a表增加一个属性
12
+ print (a .k ) -- 11,因为a有元表b,且存在元方法,可以索引到b表中对应的值
13
+ print (a .v ) -- 22,设定关系后,b增加的属性,a也可以索引到
14
+ print (b .aa ) -- nil,相反,b并不可以索引到a的值
15
+
16
+ ```
Original file line number Diff line number Diff line change 19
19
- [x] table.insert()
20
20
- [x] table.maxn()
21
21
- [x] table.concat()
22
- + [ Lua 模块与包] ( #Lua_module_package )
22
+ + [ Lua 模块与包] ( #Lua_module_package )
23
+ + [ lua中self.__ index = self 详解] ( https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Lua-Script/oop/self__index.md )
23
24
+ [ 流媒体视频直播、点播] ( #live_base_knowledge )
24
25
+ [ Nginx高性能WEB服务器详解] ( #Nginx_Web_knowledge )
25
26
+ [ 第一章 初探 ] ( #Nginx_Web1_knowledge )
You can’t perform that action at this time.
0 commit comments