lua中的类功能(面向对象2)
来源:互联网 发布:30岁程序员vs公务员 编辑:程序博客网 时间:2024/05/21 22:34
我项目中,lua使用类功能的方法:
--提供lua中class功能,目前有集成,多态的功能,析构需要实现local _class={}function bbclass(super) --参数super为父类,表示继承local class_type={}class_type.constructor=false class_type.destructor=falseclass_type.super=super --父类class_type.new=function(...) --创建对象方法,返回该类一个新对象local obj={}dolocal createcreate = function(c,...)if c.super then --如果这个类有父类create(c.super,...)endif c.constructor then --构造函数不为空--构造函数,dnmission:constructor()相当于dnmission.construct(self)c.constructor(obj,...) endend--设置元表,使对象字段未赋值时,读类的默认值setmetatable(obj,{ __index=_class[class_type] })create(class_type,...)endreturn objend class_type.delete=function(obj) do local remove = nil remove = function(c,...) --log(c.destructor) if c.destructor then c.destructor(obj, ...) end if c.super then remove(c.super, ...) end end --TODO:这里应该有内存泄漏 remove(class_type) end end local vtbl={}_class[class_type]=vtbl setmetatable(class_type,{__newindex=function(t,k,v)vtbl[k]=vend}) if super thensetmetatable(vtbl,{__index=function(t,k)local ret=_class[super][k]vtbl[k] = retreturn retend})end return class_typeend
dnmission = bbclass()function dnmission:constructor()print("dnmission:constructor")self.id = 0self.state = 0self.type = 0self.category = 0endfunction dnmission:destructor()print("dnmission:destructor")end
local mission=dnmission.new()
0 0
- lua中的类功能(面向对象2)
- lua中的面向对象
- lua中的面向对象
- Lua中的面向对象
- lua中的面向对象
- Lua中的面向对象[2]:使用指针
- lua中的面向对象设计
- Lua中的面向对象编程
- lua中的面向对象编程
- Lua中的面向对象编程
- Lua中的面向对象编程
- Lua中的面向对象概念
- Lua中的面向对象引导
- lua中的面向对象模拟,类,继承,多态
- lua中的面向对象模拟,类,继承,多态
- lua中的面向对象模拟,类,继承,多态
- lua中的面向对象模拟,类,继承,多态
- lua中的面向对象模拟,类,继承,多态
- 多态
- kafka (java API demo)
- Python进阶(三十)-Python3实现随机数
- “File too small for architecture arm64”, “clang: error: linker command failed with exit code 1 ”
- ARM仿真的几个概念JTAG/J-LINK/ULINK/ST-LINK和区别
- lua中的类功能(面向对象2)
- 逻辑回归中代价函数的选择.凸优化问题
- Java 如何格式化中国标准时间
- spring 日期时间提交出现400错误
- 算法----三种博弈问题
- 数据库事务的四大特性以及事务的隔离级别
- 信噪比snr与比特信噪比EbN0
- 第二章 (2)重写和继承关系中的构造方法
- flexigrid实际使用总结