Lua面向对象
发布时间:2020-12-14 22:09:38 所属栏目:大数据 来源:网络整理
导读:? ? Lua中的table就是一种对象,但是如果直接使用仍然会存在大量的问题,见如下代码: 1 Account = {balance = 0 } 2 function Account.withdraw(v) 3 Account.balance = Account.balance - v 4 end 5 -- 下面是测试调用函数 6 Account.withdraw( 100.00 ) ?
? ? Lua中的table就是一种对象,但是如果直接使用仍然会存在大量的问题,见如下代码: 1 Account = {balance = 0} 2 function Account.withdraw(v) 3 Account.balance = Account.balance - v 4 end 5 --下面是测试调用函数 6 Account.withdraw(100.00) ??? 在上面的withdraw函数内部依赖了全局变量Account,一旦该变量发生改变,将会导致withdraw不再能正常的工作,如: 1 a = Account; Account = nil
2 a.withdraw(100.00) 将会导致访问空nil的错误。
??? 这种行为明显的违反了面向对象封装性和实例独立性。要解决这一问题,我们需要给withdraw函数在添加一个参数self,他等价于Java/C++中的this,见如下修改: 1 function Account.withdraw(self,v)
2 self.balance = self.balance - v
3 下面是基于修改后代码的调用:5 a1 = Account; Account = 6 a1.withdraw(a1,正常工作。
??? 针对上述问题,Lua提供了一种更为便利的语法,即将点(.)替换为冒号(:),这样可以在定义和调用时均隐藏self参数,如: function Account:withdraw(v)
调用代码可改为:5 a:withdraw( ????1. 类: ?? ?Lua在语言上并没有提供面向对象的支持,因此想实现该功能,我们只能通过table来模拟,见如下代码及关键性注释: 1 --[[ 2 在这段代码中,我们可以将Account视为class的声明,如Java中的: 3 public class Account 4 { 5 public float balance = 0; 6 public Account(Account o); 7 public void deposite(float f); 8 } 9 ]] 10 这里balance是一个公有的成员变量。11 Account = {balance = 12 13 new可以视为构造函数14 function Account:new(o) 15 o = o or {} 如果参数中没有提供table,则创建一个空的。16 将新对象实例的metatable指向Account表(类),这样就可以将其视为模板了。17 setmetatable(o,self) 18 在将Account的__index字段指向自己,以便新对象在访问Account的函数和字段时,可被直接重定向。19 self.__index = self 20 最后返回构造后的对象实例21 return o 22 23 24 deposite被视为Account类的公有成员函数25 function Account:deposit(v) 26 这里的self表示对象实例本身27 self.balance = self.balance + v 28 29 30 下面的代码创建两个Account的对象实例31 32 通过Account的new方法构造基于该类的示例对象。33 a = Account:new() 34 35 这里需要具体解释一下,此时由于table a中并没有deposite字段,因此需要重定向到Account, 36 同时调用Account的deposite方法。在Account.deposite方法中,由于self(a对象)并没有balance 37 字段,因此在执行self.balance + v时,也需要重定向访问Account中的balance字段,其缺省值为0。 38 在得到计算结果后,再将该结果直接赋值给a.balance。此后a对象就拥有了自己的balance字段和值。 39 下次再调用该方法,balance字段的值将完全来自于a对象,而无需在重定向到Account了。 40 41 a:deposit(100.00) 42 print(a.balance) 输出10043 44 b = Account:new() 45 b:deposit(200.00) 46 print(b.balance) 输出200 需要说明的是,这段代码仅提供和继承相关的注释,和类相关的注释在上面的代码中已经给出。 2 Account = {balance = 3
5 o = o or {}
6 7 self.__index = self
8 10
11 12 self.balance = self.balance + v
14
15 if v > self.balance then
17 error("Insufficient funds")
19 self.balance = self.balance - v
20 21
下面将派生出一个Account的子类,以使客户可以实现透支的功能。23 SpecialAccount = Account:new() 此时SpecialAccount仍然为Account的一个对象实例24
派生类SpecialAccount扩展出的方法。26 下面这些SpecialAccount中的方法代码(getLimit/withdraw),一定要位于SpecialAccount被Account构造之后。27 function SpecialAccount:getLimit()
28 此时的self将为对象实例。29 return self.limit or 0
SpecialAccount将为Account的子类,下面的方法withdraw可以视为SpecialAccount33 重写的Account中的withdraw方法,以实现自定义的功能。function SpecialAccount:withdraw(v)
35 36 if v - self.balance >= self:getLimit() 37 38 39 self.balance = self.balance - v
41
42 在执行下面的new方法时,table s的元表已经是SpecialAccount了,而不再是Account。43 s = SpecialAccount:new{limit = 1000.00}
44 在调用下面的deposit方法时,由于table s和SpecialAccount均未提供该方法,因此访问的仍然是45 Account的deposit方法。46 s:deposit(100)
47
48
49 此时的withdraw方法将不再是Account中的withdraw方法,而是SpecialAccount中的该方法。50 这是因为Lua先在SpecialAccount(即s的元表)中找到了该方法。51 s:withdraw(52 print(s.balance) 输出-100
|