lua继承
来源:互联网 发布:周青杨的淘宝网店 编辑:程序博客网 时间:2024/06/02 05:19
第一部分:LUA的继承
充电站:元表
- lua中的元表和js的原型非常相似,熟悉js的朋友应该发现了
- 在lua中,每一个表都有TA的元表metatable,lua默认创建一个不带元表的新表:
t = {}print(getmetatable(t)) --nil
- 设置元表
mt = {name = "quick"}t = {}setmetatable(t, mt)assert(getmetatalbe(t) == mt)
图示代码:
--====================Person======================local Person = {}Person.attack = 5function Person:new(o) o = o or {} setmetatable(o, self) self.__index = self return oendfunction Person:setAttack(attack) self.attack = attackendfunction Person:getAttack() return self.attackend--====================Hero======================local Hero = Person:new()Hero.name = ""Hero.skill = ""--====================hero1,hero2======================local hero1 = Hero:new()hero1.name = "金刚狼"hero1.skill = "甩开爪子切牛排"local hero2 = Hero:new({name = "超人"})hero2.skill = "内裤外穿走T台"----====================================================function printKeys(name, t) print("======================" .. name) for k, v in pairs(t) do print(k) endendprintKeys("Person", Person)printKeys("Person.__index", Person.__index)printKeys("Hero", Hero)printKeys("Hero.__index", Hero.__index)printKeys("getmetatable(Hero).__index", getmetatable(Hero).__index)printKeys("hero1", hero1)printKeys("hero2", hero2)
结合log我们分析下:
======================PersonsetAttack__indexgetAttacknewattack======================Person.__indexsetAttack__indexgetAttacknewattack======================Heroskillname__index======================Hero.__indexskillname__index======================getmetatable(Hero).__indexsetAttack__indexgetAttacknewattack======================hero1nameskill======================hero2nameskill[Finished in 0.0s]
转回正题
假如当我们调用hero1:setAttack(500)
的时候,在hero1中是找不到setAttack
方法的,这时候:
- lua会通过
getmetatable(hero1)
得到hero1
的元表并到元表的__index域
中去查找,箭头走向:**3---》2** - 但仍然没找到,得到Hero元表并继续在其
__index域
中寻找,箭头走向:**1---》0**,这时候寻找到setAttack
方法并且调用,由于setAttack
方法,hero1
会增加字段attack
。
第二部分:QUICK的继承
quick的继承实现要考虑到对C++对象的继承和对lua对象的继承。对lua对象的继承我们第一部分已经用元表机制说明。当我们在quick中用class新建类时,始终要清醒的明白,我们新建的类其实就是返回一个lua表(cls).
继承的核心代码见framework/functions.lua
中class(classname, super)
函数。
为了方便理解,上个图先:
函数class(classname, super)有两个参数:
- 参数1:classname,见名知意,类名
- 参数2:super
- 1.super的类型:
superType = type(super)
- 2.superType可以为
function
,table
,当不为这两种类型的时候我们将之置为nil
- 3.superType为
function
的时候,表示从C++对象继承,走图示2 - 4.superType为
table
的时候,还要看其__ctype
值,1表示继承自C++对象,走图1;2表示继承自lua表对象,走图3 - 5.superType为
nil
的时候,从lua表继承,走图4
- 1.super的类型:
samples/coinFlip项目是个绝佳的例子,我们可以结合该范例进行理解。这里我就不赘述了。
更新:在谷歌邮件组Jacky的回复中,提到了另外一个朋友写的文章,非常不错,针对本文的第二部分讲解的很透彻,地址
你我是朋友,各拿一个苹果彼此交换,交换后仍然是各有一个苹果;倘若你有一个思想,我也有一种思想,而朋友间交流思想,那我们每个人就有两种思想了。 ——爱尔兰剧作家 萧伯纳
0 0
- lua继承
- lua继承
- lua继承
- lua 类、继承、多重继承
- lua 类与继承
- lua 之多重继承
- lua实现继承
- lua 多重继承
- lua继承c++类
- Lua 多重继承
- [读书笔记] Lua多重继承
- lua setmetatable实现继承
- lua实现继承、多态
- lua 多继承实现
- lua类的继承
- Lua模拟继承行为
- 【步兵 lua】模拟继承
- lua实现伪多态继承
- 用display_raw看字段的直方图
- 使用 WebSphere 配置 Web 服务安全性
- Android NDK开发环境搭建
- DG(1)Performing a Failover to a Physical Standby Database
- Spring常用的Listener
- lua继承
- CDN(内容分发网络)技术原理
- 打动面试官的五大技能
- GCD
- C语言格式转换说明符
- 一切成功源于积累——20140228 确定交易时间很重要 也就是入场点
- 表单提交验证
- js 判断js函数、变量是否存在
- How does Impala compare to Shark?