lua的私有性(privacy)

Lua没有打算被用来进行大型的程序设计,相反,Lua目标定于小型到中型的程序设计,通常是作为大型系统的一部分。

典型的,被一个或者很少几个程序员开发,甚至被非程序员使用。所以,Lua避免太冗余和太多的人为限制。如果你不

想访问一个对象内的一些东西就不要访问(If you do not want to access something inside an object, just do not do it.)。

 1 function newAccount (initialBalance)
 2 
 3     local self = {balance = initialBalance}
 4 
 5     local withdraw = function (v)
 6 
 7        self.balance = self.balance - v
 8 
 9     end
10 
11     local deposit = function (v)
12 
13        self.balance = self.balance + v
14 
15     end
16 
17     local getBalance = function () return self.balance end
18 
19     return {
20 
21        withdraw = withdraw,
22 
23        deposit = deposit,
24 
25        getBalance = getBalance
26 
27     }
28 
29 end
30 
31 
32 acc1 = newAccount(100.00)
33 
34 acc1.withdraw(40.00)
35 

36 print(acc1.getBalance())    --> 60 

 

posted @ 2011-08-11 11:23  麦飞  阅读(372)  评论(0编辑  收藏  举报