lua的私有性(privacy)
function newAccount (initialBalance)
local self = { balance = initialBalance }
local withdraw = function (v)
self.balance = self.balance - v
end local deposit = function (v)
self.balance = self.balance + v
end local getBalance = function ()
return self.balance
end return {
withdraw = withdraw,
deposit = deposit,
getBalance = getBalance
}
end acc1 = newAccount(100.00)
acc1.withdraw(40.00) print(acc1.getBalance()) -- 60
acc1.deposit()
print(acc1.getBalance()) -- 100 print(acc1.balance) -- nil
首先,函数创建一个表用来描述对象的内部状态,并保存在局部变量self内。然后,函数为对象的每一个方法创建闭包(也就是说,嵌套的函数实例)。最后,函数创建并返回外部对象,外部对象中将局部方法名指向最终要实现的方法。这儿的关键点在于:这些方法没有使用额外的参数self,代替的是直接访问self。因为没有这个额外的参数,我们不能使用冒号语法来访问这些对象。函数只能像其他函数一样调用: acc1.deposit(40) acc1.getBalance()
这种设计实现了任何存储在self表中的部分都是私有的,newAccount返回之后,没有什么方法可以直接访问对象,我们只能通过newAccount中定义的函数来访问他。虽然我们的例子中仅仅将一个变量放到私有表中,但是我们可以将对象的任何的部分放到私有表中。我们也可以定义私有方法,他们看起来象公有的,但我们并不将其放到接口中。例如,我们的账号可以给某些用户取款享有额外的10%的存款上限,但是我们不想用户直接访问这种计算的详细信息,我们实现如下:
function newAccount (initialBalance)
local self = { balance = initialBalance ,
LIM = } local withdraw = function (v)
self.balance = self.balance - v
end local extra = function ()
if self.balance > self.LIM then
return self.balance*0.10
else
return
end
end local deposit = function (v)
self.balance = self.balance + v
end local getBalance = function ()
return self.balance + extra() --[此处非self.extra()]
end return {
withdraw = withdraw,
deposit = deposit,
getBalance = getBalance,
-- extra = extra,
}
end
这样,对于用户而言就没有办法直接访问extra函数了;如此 也就实现lua private function。
lua的私有性(privacy)的更多相关文章
- 2015/9/28 Python基础(19):类的定制和私有性
用特殊方法定制类前面我们讲了方法的两个重要方面:首先,方法必须在调用前被绑定(到它们相应类的某个实例中):其次,有两个特殊方法可以分别作为构造器和解构器的功能,分别名为__init__()和__del ...
- Rust中的模块及私有性控制
好像没有其它语言的private, protected关键字,应了一个public关键字. mod plant { pub struct Vegetable { pub name: String, _ ...
- Lua语言中文手册 转载自网络
Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...
- Lua学习笔记一
学习了有一周多了.之前一直不想献丑,但还是记录下这个过程. 第1章 开发软件搭建 1. ubuntu 下lua安装 sudo apt-get install lua5.1 2.win下的环境搭建. ...
- Lua面向对象设计
首先对于Lua语言,它没有打算被用来进行大型的程序设计,相反,Lua目标定于小型到中型的程序设计,通常是作为大型系统的一部分,所以它只提供了一套精简的元素,很多高级语言的概念都没有.这样Lua就成为了 ...
- Lua基础之table详解
概要:1.table特性:2.table的构造:3.table常用函数:4.table遍历:5.table面向对象 原文地址:http://blog.csdn.net/dingkun520wy/art ...
- Lua面向对象设计(转)
首先对于Lua语言,它没有打算被用来进行大型的程序设计,相反,Lua目标定于小型到中型的程序设计,通常是作为大型系统的一部分,所以它只提供了一套精简的元素,很多高级语言的概念都没有.这样Lua就成为了 ...
- lua的面向对象
Lua中的table就是一种对象,但是如果直接使用仍然会存在大量的问题,见如下代码: Account = {balance = } function Account.withdraw(v) Accou ...
- Lua的面向对象程序设计
Account={balance=} function Account.withdraw(self,v) self.balance=self.balance-v end a={balance=,wit ...
随机推荐
- c# DataTable 转为 List 类型
代码: public class ModelConvertHelper<T> where T : new() { public static IList<T> ConvertT ...
- 使用CSS设置行间距,字间距.
字间距1.text-indent设置抬头距离css缩进即对,对应div设置css样式text-indent : 20px; 缩进了20px 2.letter-spacing来设置字与字间距_字符间距离 ...
- [LeetCode]436 Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- invalidate()和postInvalidate()的使用与区别
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型: Android UI操作并不是线程安全的,并且这些操作必须在UI线程 ...
- SQL之DDL
DDL是SQL定义语言,它主要包括三个关键字:create ,alter , drop(数据库关键字不分大小写 ),主要操作对象 有数据库.表.索引.视图等 操作 ...
- SQL SERVER 2000数据库置疑处理
由于服务器意外的断电,导致SQL SERVER服务器上数据库出现“置疑”而无法使用,通过网上搜索,找到以下方法解决问题,这里记录一下: 产生数据库置疑的时侯,数据库文件和日志文件都是存在的,如果数据库 ...
- petapoco 使用 MiniProfiler Glimpse监控
PetaPoco是一款适用于.Net(window) 和Mono( linux )的微小.快速.单文件的微型ORM. MVC MiniProfiler是Stack Overflow团队设计的一款对AS ...
- 形参是ofstream
今天写了一段代码报错 void GetEigenvalue(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud, vector<int> ...
- 【C-数据类型 常量 变量】
一.基本数据类型 1) 整型 (int %d) 2) 字符型 (char %c) 3) 浮点型 %f ①. 单精度浮点型(float) ②. 双精度浮点型(double) 2.指针类型 void ...
- Secret Codes
Secret Codes This is a list of codes that can be entered into the dialer to output the listed info ...