Lua中的table就是一种对象,但是如果直接使用仍然会存在大量的问题,见如下代码:

 Account = {balance = }
function Account.withdraw(v)
Account.balance = Account.balance - v
end
--下面是测试调用函数
Account.withdraw(100.00)

Lua提供了一种更为便利的语法,即将点(.)替换为冒号(:),这样可以在定义和调用时均隐藏self参数,如

 function Account:withdraw(v)
self.balance = self.balance - v
end
--调用代码可改为:
a:withdraw(100.00)

类的定义

--[[
在这段代码中,我们可以将Account视为class的声明,如Java中的:
public class Account
{
public float balance = 0;
public Account(Account o);
public void deposite(float f);
}
--]]
--这里balance是一个公有的成员变量。
Account = {balance = } --new可以视为构造函数
function Account:new(o)
o = o or {} --如果参数中没有提供table,则创建一个空的。
--将新对象实例的metatable指向Account表(类),这样就可以将其视为模板了。
setmetatable(o,self)
--在将Account的__index字段指向自己,以便新对象在访问Account的函数和字段时,可被直接重定向。
self.__index = self
--最后返回构造后的对象实例
return o
end --deposite被视为Account类的公有成员函数
function Account:deposit(v)
--这里的self表示对象实例本身
self.balance = self.balance + v
end --下面的代码创建两个Account的对象实例 --通过Account的new方法构造基于该类的示例对象。
a = Account:new()
--[[
这里需要具体解释一下,此时由于table a中并没有deposite字段,因此需要重定向到Account,
同时调用Account的deposite方法。在Account.deposite方法中,由于self(a对象)并没有balance
字段,因此在执行self.balance + v时,也需要重定向访问Account中的balance字段,其缺省值为0。
在得到计算结果后,再将该结果直接赋值给a.balance。此后a对象就拥有了自己的balance字段和值。
下次再调用该方法,balance字段的值将完全来自于a对象,而无需在重定向到Account了。
--]]
a:deposit(100.00)
print(a.balance) --输出100 b = Account:new()
b:deposit(200.00)
print(b.balance) --输出200

继承

--需要说明的是,这段代码仅提供和继承相关的注释,和类相关的注释在上面的代码中已经给出。
Account = {balance = } function Account:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o
end function Account:deposit(v)
self.balance = self.balance + v
end function Account:withdraw(v)
if v > self.balance then
error("Insufficient funds")
end
self.balance = self.balance - v
end --下面将派生出一个Account的子类,以使客户可以实现透支的功能。
SpecialAccount = Account:new() --此时SpecialAccount仍然为Account的一个对象实例 --派生类SpecialAccount扩展出的方法。
--下面这些SpecialAccount中的方法代码(getLimit/withdraw),一定要位于SpecialAccount被Account构造之后。
function SpecialAccount:getLimit()
--此时的self将为对象实例。
return self.limit or
end --SpecialAccount将为Account的子类,下面的方法withdraw可以视为SpecialAccount
--重写的Account中的withdraw方法,以实现自定义的功能。
function SpecialAccount:withdraw(v)
--此时的self将为对象实例。
if v - self.balance >= self:getLimit() then
error("Insufficient funds")
end
self.balance = self.balance - v
end --在执行下面的new方法时,table s的元表已经是SpecialAccount了,而不再是Account。
s = SpecialAccount:new{limit = 1000.00}
--在调用下面的deposit方法时,由于table s和SpecialAccount均未提供该方法,因此访问的仍然是
--Account的deposit方法。
s:deposit() --此时的withdraw方法将不再是Account中的withdraw方法,而是SpecialAccount中的该方法。
--这是因为Lua先在SpecialAccount(即s的元表)中找到了该方法。
s:withdraw(200.00)
print(s.balance) --输出-100

封装

--这里我们需要一个闭包函数作为类的创建工厂
function newAccount(initialBalance)
--这里的self仅仅是一个普通的局部变量,其含义完全不同于前面示例中的self。
--这里之所以使用self作为局部变量名,也是为了方便今后的移植。比如,以后
--如果改为上面的实现方式,这里应用了self就可以降低修改的工作量了。
local self = {balance = initialBalance} --这里我们可以将self视为私有成员变量
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
--返回对象中包含的字段仅仅为公有方法。事实上,我们通过该种方式,不仅可以实现
--成员变量的私有性,也可以实现方法的私有性,如:
--local privateFunction = function() --do something end
--只要我们不在输出对象中包含该方法的字段即可。
return {withdraw = withdraw, deposit = deposit, getBalance = getBalance}
end --和前面两个示例不同的是,在调用对象方法时,不再需要self变量,因此我们可以直接使用点(.),
--而不再需要使用冒号(:)操作符了。
accl = newAccount(100.00)
--在函数newAccount返回之后,该函数内的“非局部变量”表self就不再能被外部访问了,只能通过
--该函数返回的对象的方法来操作它们。
accl.withdraw(40.00)
print(acc1.getBalance())

最新文章

  1. Object-c 内存管理
  2. C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配
  3. python机器学习《回归 一》
  4. mysql启动报错(mac)
  5. bzoj 2751 快速幂
  6. How to use Mac Terminal
  7. linux性能调优概述
  8. COJ 0560 4015划分数
  9. 【2】Chrome - 快捷键
  10. SEO,搜索引擎优化原理方法等整体把握
  11. hibernate的基本配置
  12. async和enterproxy控制并发数量
  13. .NET core RSA帮助类
  14. Mac系统在Pycharm中切换解释器
  15. Python全栈之路----函数进阶----闭包
  16. mail.jar 发送邮件
  17. Lintcode: Implement Queue by Stacks 解题报告
  18. 高版本的jdk编译过的项目移到低版本的JDK的eclipse中出错的问题
  19. sprint3(第一天)
  20. IIS中User-mode caching引起的Cache-Control不为public的问题

热门文章

  1. python_flask 基础巩固 (URL_FOR 详解)
  2. python猜数字游戏console版本
  3. 注册MongoDB为系统服务(二)
  4. The test form is only available for requests from the local machine
  5. 【分布式缓存系列】集群环境下Redis分布式锁的正确姿势
  6. 使用Python多渠道打包apk
  7. Vmware12 u盘启动系统
  8. Java 线程池的原理及实现
  9. object标签和embed标签
  10. [Postman]证书(13)