在lua中创建类和对象

来源:互联网 发布:外交豁免权知乎 编辑:程序博客网 时间:2024/06/04 17:40
Account = {balance = 10}function Account:withdraw (v)self.balance = self.balance - vendfunction Account:deposit (v)self.balance = self.balance + vendfunction Account:create()local o = {}setmetatable(o, self)self.__index = selfreturn oenda = Account:create()--重写基类方法function a:deposit(v)print("hello world")endprint(a.balance)a:deposit(100)
lua中没有类,但通过setmetatable和__index可以仿制一个类,a就从Account继承了所有的方法和变量,还可以重写方法
原创粉丝点击