Skip to content

Commit a76986b

Browse files
committed
oop3
1 parent cda514f commit a76986b

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

Lua-Script/oop/account/account.lua

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: Administrator
4+
-- Date: 2017/6/6
5+
-- Time: 17:14
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
local _M = {}
9+
10+
local mt = { __index = _M }
11+
12+
function _M.deposit (self, v)
13+
self.balance = self.balance + v
14+
end
15+
16+
function _M.withdraw (self, v)
17+
if self.balance > v then
18+
self.balance = self.balance - v
19+
else
20+
error("insufficient funds")
21+
end
22+
end
23+
24+
function _M.new (self, balance)
25+
balance = balance or 0
26+
return setmetatable({balance = balance}, mt)
27+
end
28+

Lua-Script/oop/class01.lua

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--
2+
local Sharp = { _val = 1 } --① 父类
3+
4+
function Sharp:new()
5+
local new_sharp = {}
6+
self.__index = self --②,self == Sharp Sharp.__index = Sharp 等价于 Sharp.__index = function(key) return Sharp[key] end
7+
setmetatable(new_sharp, self)
8+
return new_sharp
9+
end
10+
11+
-- define fun1
12+
function Sharp:sharp_func()
13+
print("Sharp call sharp_func")
14+
end
15+
16+
-- define fun2
17+
function Sharp:name()
18+
print("Sharp call name")
19+
end
20+
21+
-- define fun3
22+
function Sharp:val()
23+
print(string.format("Sharp call val %d", self._val))
24+
end
25+
26+
local sharp = Sharp:new()
27+
sharp:sharp_func()
28+
sharp:name()
29+
sharp:val()

Lua-Script/oop/class03.lua

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
-- 类的继承
2+
local Sharp = { _val = 1 } --① 父类
3+
4+
function Sharp:new()
5+
local new_sharp = {}
6+
self.__index = self --②,self == Sharp Sharp.__index = Sharp 等价于 Sharp.__index = function(key) return Sharp[key] end
7+
setmetatable(new_sharp, self)
8+
return new_sharp
9+
end
10+
11+
-- define fun1
12+
function Sharp:sharp_func()
13+
print("Sharp call sharp_func")
14+
end
15+
16+
-- define fun2
17+
function Sharp:name()
18+
print("Sharp call name")
19+
end
20+
21+
-- define fun3
22+
function Sharp:val()
23+
print(string.format("Sharp call val %d", self._val))
24+
end
25+
26+
Circle = Sharp:new() --① 子类
27+
function Circle:new()
28+
local new_circle = {}
29+
self.__index = self --②,self == Circle
30+
setmetatable(new_circle, self) --
31+
32+
return new_circle
33+
end
34+
35+
--新函数
36+
function Circle:circle_func()
37+
print("Circle call circle_func")
38+
end
39+
40+
--覆盖函数name
41+
function Circle:name()
42+
print("Circle call name")
43+
end
44+
45+
--覆盖函数val
46+
function Circle:val()
47+
print(string.format("Circle call val %d", self._val))
48+
end
49+
50+
--输出结果
51+
--Sharp call sharp_func
52+
--Circle call circle_func
53+
--Circle call name
54+
--Circle call val 2
55+
56+
--调用父类被子类覆盖了的name方法
57+
function Circle:sharp_name()
58+
local circle_metatable = getmetatable(self)
59+
-- local sharp_metatable = getmetatable(circle_metatable)
60+
-- sharp_metatable["name"]()
61+
print(circle_metatable)
62+
end
63+
64+
local circle1 = Circle:new()
65+
circle1.name()
66+
circle1.sharp_name()
67+
-- circle._val = 2 --覆盖赋值
68+
-- circle:sharp_func() --调用父类函数
69+
-- circle:circle_func() --调用新函数
70+
-- circle:name() --调用覆盖函数
71+
-- circle:val() --调用覆盖函数

Lua-Script/oop/self_class01.lua

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: Administrator
4+
-- Date: 2017/6/6
5+
-- Time: 9:47
6+
-- To change this template use File | Settings | File Templates.
7+
-- 自己尝试着写一个类哦
8+
9+
--
10+
local ok, new_tab = pcall(require, "table.new")
11+
if not ok or type(new_tab) ~= "function" then
12+
new_tab = function (narr, nrec) return {} end
13+
end
14+
15+
local _M = new_tab(0, 54) --① 父类
16+
17+
_M._VERSION = '0.01'
18+
19+
function _M:new()
20+
local new_obj = {}
21+
self.__index = self --②,self == _M
22+
setmetatable(new_obj, self)
23+
return new_obj
24+
end
25+
26+
-- 定义个简单的方法
27+
function _M:func()
28+
print("Function func ")
29+
end
30+
31+
return _M
32+

Lua-Script/oop/self_class02.lua

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: Administrator
4+
-- Date: 2017/6/6
5+
-- Time: 9:49
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
local class01 = require "self_class01"
9+
print("_VERSION == "..class01._VERSION)
10+
local func = class01:func()
11+
print(func)
12+

0 commit comments

Comments
 (0)