基本原理
lua中表结构在查找元素时首先遍历表如果没有找到对应的元素就会通过元方法__index再次查找,当__index为表时就在这个表中查找,如果是function则执行这个方法。这一点和我们在面向对象编程中子类继承父类的属性和方法很相似。
示例
我们首先定义一个方法通过设置元表和元方法实现继承。
local function class(classname,super)
local temptable={
};
_G[classname]=temptable;
super.__index=super;--这样super在设置为temptable的元表后,在temptable找不到的元素可以在super.__index中继续查找
function super.New(...)--模拟构造函数
local temp={<