Skip to content

Commit 2589ecf

Browse files
committed
Lua面向对象(重点)
1 parent 1747176 commit 2589ecf

File tree

3 files changed

+249
-22
lines changed

3 files changed

+249
-22
lines changed

Lua-Script/oop/app3.lua

Lines changed: 0 additions & 22 deletions
This file was deleted.

Lua-Script/oop/oop3.lua

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: Administrator
4+
-- Date: 2017/5/25
5+
-- Time: 17:02
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
----------------------------------------------------------- Lua面向对象1
9+
-- local mt = {}
10+
-- mt.__add = function(t1, t2)
11+
-- print("两个Table 相加的时候会调用我")
12+
-- end
13+
-- local t1 = {}
14+
-- local t2 = {}
15+
--- - 给两个table 设置新的元表,一个元表就是一个table的值
16+
-- setmetatable(t1, mt) -- meta:元素
17+
-- setmetatable(t2, mt)
18+
---- 进行相加操作
19+
-- local t = t1 + t2
20+
-- print(t)
21+
22+
--[[输出结果
23+
两个Table 相加的时候会调用我
24+
nil
25+
--]]
26+
27+
----------------------------------------------------------- Lua面向对象2
28+
-- 创建一个元表 (是创建一个类吗?)
29+
local mt = {}
30+
mt.__add = function(s1, s2)
31+
local result = ""
32+
if s1.sex == "boy" and s2.sex == "girl" then
33+
result = "一个男孩和一个女孩的家庭"
34+
elseif s1.sex == "girl" and s2.sex == "girl" then
35+
result = "两个女孩的家庭"
36+
else
37+
result = "未知孩子的家庭"
38+
end
39+
return result
40+
end
41+
-- 创建两个table,可以想象成是两个类的对象(实例化两个类)
42+
local s1 = { name = "Per1", sex = "boy" }
43+
local s2 = { name = "Per2", sex = "girl" }
44+
-- 给两个table 设置新的元表,一个元表就是一个table的值
45+
setmetatable(s1, mt)
46+
setmetatable(s2, mt)
47+
-- 进行加法操作
48+
local result = s1 + s2
49+
print(result) -- 一个男孩和一个女孩的家庭
50+
51+
----------------------------------------------------------- Lua面向对象2
52+
local t = {
53+
name = "Tinywan"
54+
}
55+
local mt = {
56+
__index = function(table, key)
57+
print("虽然你调用了我不存在的字段和方法,不过没关系,我能检测出来" .. key)
58+
end
59+
}
60+
setmetatable(t, mt)
61+
print(t.name)
62+
print(t.age)
63+
--[[输出结果
64+
-- Tinywan
65+
-- 虽然你调用了我不存在的字段和方法,不过没关系,我能检测出来age
66+
-- nil
67+
---- ]]
68+
69+
----------------------------------------------------------- Lua面向对象2
70+
local t = {
71+
name = "Tinywan"
72+
}
73+
local mt = {
74+
money = 808080
75+
}
76+
77+
mt.__index = mt
78+
setmetatable(t, mt)
79+
print(t.money)
80+
-- 输出结果 808080
81+
82+
------------------------------------------------------------- Lua面向对象2
83+
local t = {
84+
name = "Tinywan"
85+
}
86+
local mt = {
87+
__index = {
88+
money = 909090
89+
}
90+
}
91+
setmetatable(t, mt)
92+
print(t.money)
93+
-- 输出结果 909090
94+
95+
----------------------------------------------------------- Lua面向对象2
96+
local smartMan = {
97+
name = "Tinywan",
98+
age = 26,
99+
money = 800000,
100+
say_fun = function()
101+
print("Tinywan say 大家好")
102+
end
103+
}
104+
105+
local t1 = {}
106+
local t2 = {}
107+
local mt = { __index = smartMan } -- __index 可以是一个函数,也可以是一个函数
108+
setmetatable(t1, mt)
109+
setmetatable(t2, mt)
110+
print(t1.money)
111+
t2.say_fun()
112+
--- 输出结果
113+
-- 800000
114+
-- Tinywan say 大家好
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,129 @@
368368
print('函数执行出错了')
369369
print('错误信息:',err)
370370
end
371+
```
372+
+ Lua面向对象(重点)
373+
+ [博客详细地址描述](http://www.cnblogs.com/tinywan/p/6940784.html)
374+
+ :white_check_mark: `__add` 元方法 #demo1
375+
```lua
376+
local mt = {}
377+
mt.__add = function(t1, t2)
378+
print("两个Table 相加的时候会调用我")
379+
end
380+
local t1 = {}
381+
local t2 = {}
382+
-- 给两个table 设置新的元表,一个元表就是一个table的值
383+
setmetatable(t1, mt) -- meta:元素
384+
setmetatable(t2, mt)
385+
-- 进行相加操作
386+
local t = t1 + t2
387+
print(t)
388+
389+
--[[输出结果
390+
两个Table 相加的时候会调用我
391+
nil
392+
--]]
393+
```
394+
+ :white_check_mark: `__add` 元方法 #demo2
395+
```lua
396+
-- 创建一个元表 (是创建一个类吗?)
397+
local mt = {}
398+
mt.__add = function(s1, s2)
399+
local result = ""
400+
if s1.sex == "boy" and s2.sex == "girl" then
401+
result = "一个男孩和一个女孩的家庭"
402+
elseif s1.sex == "girl" and s2.sex == "girl" then
403+
result = "两个女孩的家庭"
404+
else
405+
result = "未知孩子的家庭"
406+
end
407+
return result
408+
end
409+
-- 创建两个table,可以想象成是两个类的对象(实例化两个类)
410+
local s1 = { name = "Per1", sex = "boy" }
411+
local s2 = { name = "Per2", sex = "girl" }
412+
-- 给两个table 设置新的元表,一个元表就是一个table的值
413+
setmetatable(s1, mt)
414+
setmetatable(s2, mt)
415+
-- 进行加法操作
416+
local result = s1 + s2
417+
print(result)
418+
419+
-- 输出结果 一个男孩和一个女孩的家庭
420+
```
421+
+ :white_check_mark: `__index` 元方法 #demo1
422+
```lua
423+
local t = {
424+
name = "Tinywan"
425+
}
426+
local mt = {
427+
__index = function(table, key)
428+
print("虽然你调用了我不存在的字段和方法,不过没关系,我能检测出来" .. key)
429+
end
430+
}
431+
setmetatable(t, mt)
432+
print(t.name)
433+
print(t.age)
434+
435+
--[[输出结果
436+
-- Tinywan
437+
-- 虽然你调用了我不存在的字段和方法,不过没关系,我能检测出来age
438+
-- nil
439+
---- ]]
371440
```
441+
+ :white_check_mark: `__index` 元方法 #demo2
442+
```lua
443+
local t = {
444+
name = "Tinywan"
445+
}
446+
local mt = {
447+
money = 808080
448+
}
449+
450+
mt.__index = mt
451+
setmetatable(t, mt)
452+
print(t.money)
453+
-- 输出结果 808080
454+
```
455+
+ :white_check_mark: `__index` 元方法 #demo3
456+
```lua
457+
local t = {
458+
name = "Tinywan"
459+
}
460+
local mt = {
461+
__index = {
462+
money = 909090
463+
}
464+
}
465+
setmetatable(t, mt)
466+
print(t.money)
467+
-- 输出结果 909090
468+
```
469+
+ :white_check_mark: `__index` 元方法 #demo4
470+
```lua
471+
local smartMan = {
472+
name = "Tinywan",
473+
age = 26,
474+
money = 800000,
475+
say_fun = function()
476+
print("Tinywan say 大家好")
477+
end
478+
}
479+
480+
local t1 = {}
481+
local t2 = {}
482+
local mt = { __index = smartMan } -- __index 可以是一个函数,也可以是一个函数
483+
setmetatable(t1, mt)
484+
setmetatable(t2, mt)
485+
print(t1.money)
486+
t2.say_fun()
487+
--- 输出结果
488+
-- 800000
489+
-- Tinywan say 大家好
490+
```
491+
+ Lua面向对象1
492+
+ Lua面向对象1
493+
+ Lua面向对象3 更新中...
372494
#### 控制结构
373495
+ [if-elseif-end 语句](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Lua-Script/chapter-one/if-else-example.lua)
374496
+ [for 语句](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Lua-Script/chapter-one/for-example.lua)

0 commit comments

Comments
 (0)