Skip to content

Commit 4d1ae31

Browse files
committed
encapsulation chapter: lua => DONE
1 parent 560e2f6 commit 4d1ae31

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

book/encapsulation.tex

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ \subsection{Javascript}
8686

8787
\subsection{Lua}
8888

89-
%\lstinputlisting[language={[5.2]Lua}, linerange={1-2}, style=codeStyle]{../codes/lua/person.lua}
89+
Using tables to create objects, all members are accessible outside the class, hence public.
90+
91+
\lstinputlisting[language={[5.2]Lua}, linerange={1-5,7-7,11-12,14-14,17-19}, style=codeStyle]{../codes/lua/encapsulation/person.lua}
92+
93+
You can access them anywhere
94+
95+
\lstinputlisting[language={[5.2]Lua}, linerange={1-6}, style=codeStyle]{../codes/lua/encapsulation/app.lua}
9096

9197
\subsection{Perl}
9298

@@ -182,7 +188,7 @@ \subsection{Javascript}
182188

183189
\subsection{Lua}
184190

185-
%\lstinputlisting[language={[5.2]Lua}, linerange={1-2}, style=codeStyle]{../codes/lua/person.lua}
191+
There is no protected members in Lua, but you can use a naming convention to specify them.
186192

187193
\subsection{Perl}
188194

@@ -300,7 +306,15 @@ \subsection{Javascript}
300306

301307
\subsection{Lua}
302308

303-
%\lstinputlisting[language={[5.2]Lua}, linerange={1-2}, style=codeStyle]{../codes/lua/person.lua}
309+
Using closures, you can create internal fields inside the constructor of an object.
310+
The functions which access this field must be defined inside the constructor.
311+
312+
\lstinputlisting[language={[5.2]Lua}, linerange={1-6,8-12,19-19}, style=codeStyle]{../codes/lua/encapsulation/person.lua}
313+
314+
You cannot access it outside the class.
315+
But, you can create a new public field dynamically, but it will not replace the internal one.
316+
317+
\lstinputlisting[language={[5.2]Lua}, linerange={1-3,7-8}, style=codeStyle]{../codes/lua/encapsulation/app.lua}
304318

305319
\subsection{Perl}
306320

codes/lua/encapsulation/app.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local Person = require "person"
2+
3+
local p = Person.new("Karim_p")
4+
p:info()
5+
6+
p.luckyNumber = 6
7+
-- print("name outside= " .. p.name) -- error: nil value
8+
p.name = "other name"
9+
p:info()

codes/lua/encapsulation/encap.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local Index = {
2+
local private = {}
3+
local protected = {}
4+
}
5+
6+
Index.__index = function(self, key)
7+
local member = Rectangle[key]
8+
if member ~= nil then
9+
return member
10+
end
11+
if key == "w" then
12+
return self.width + 2
13+
elseif key == "h" then
14+
return self.height + 1
15+
end
16+
end
17+
18+
Index.__newindex = function(self, key, value)
19+
if string.sub(key,1,2) == "__" then
20+
21+
elseif string.sub(key,1,1) == "_" then
22+
end
23+
end
24+
25+
local Person = {}
26+
Person.__index = Person
27+
28+
function Person.new(name) -- The constructor
29+
local self = {}
30+
local _name = name;
31+
self.luckyNumber = 1;
32+
self.info1 = function()
33+
print("My name: " .. _name)
34+
end
35+
return setmetatable(self, Person)
36+
end
37+
38+
function Person:info()
39+
self.info1()
40+
print("My lucky number: " .. self.luckyNumber)
41+
end

codes/lua/encapsulation/person.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local Person = {}
2+
Person.__index = Person
3+
4+
function Person.new(name) -- The constructor
5+
local self = {}
6+
local _name = name;
7+
self.luckyNumber = 1;
8+
self.info1 = function()
9+
print("My name: " .. _name)
10+
end
11+
return setmetatable(self, Person)
12+
end
13+
14+
function Person:info()
15+
self.info1()
16+
print("My lucky number: " .. self.luckyNumber)
17+
end
18+
19+
return Person

0 commit comments

Comments
 (0)