Lua面向对象之类和继承浅析
Lua中的table就是一种对象,但是如果直接使用仍然会存在大量的问题,如下:
Account={balance=0} functionAccount.withdraw(v) Account.balance=Account.balance-v end --下面是测试调用函数 Account.withdraw(100.00)
在上面的withdraw函数内部依赖全局变量Account,一旦发生改变,将会导致withdraw不能正常工作,如:
a=Account;Account=nil a.withdraw(100.00) --将会导致访问空nil的错误。
这种行为明显违反了面向对象封装性和实例独立性。要解决这一问题,我们需要给withdraw函数再添加一个参数self,他等价于java/C++中的this,如下:
functionAccount.withdraw(self,v) self.balance=self.balance-v end --下面是基于修改后代码的调用: a1=Account;Account=nil a1.withdraw(a1,100.00) --正常工作。
针对上述问题,lua提供了一种更为便利的语法,即将点(.)替换为冒号(:),这样可以在定义和调用函数时隐藏参数。如:
functionAccount:withdraw(v) self.balance=self.balance-v end --调用代码可改为: a:withdraw(100.00)
1、类:
lua中再语言上并没有提供面向对象的支持,因此想实现该功能,我们只能通过table来模拟,如下:
--这里的lovenumber是一个公有成员变量 Father={lovenumber=0}
--new可以视为构造函数 functionFather:new(p) p=por{} --如果参数中没有提供table,则创建一个空table --将新对象实例的元表指向Father,这样就可以以Father为模板了 setmetatable(p,self) --将Father的__index字段指向自己,以便新对象在找不到指定的key时可以被重定向,即访问Father拥有的key self.__index=self returnp end
functionFather:toString() print("Ilovemyson!") end
--Loving被视为公有成员函数 functionFather:Loving(v) self.lovenumber=self.lovenumber+v--这里的self表示实例对象本身 returnself.lovenumber end
f1=Father:new{name="jianjian"} f2=Father:new{name="baba",} print(f1:Loving(100)) print(f2:Loving(200)) --输出答案 --100 --200