--因为只有当读写不存在的域时,才会触发__index和__newindex
classA = {className = "classA",name="classAInst"}
function classA:new(newobject)
newobject = newobject or {}
setmetatable(newobject, {__index = self})--当在newobject找不到key对应的value时,就到self(即classA)类中查找
return newobject
end
function inherit(p)
local newclass = {className = "classB",parent = p}
setmetatable(newclass,{__index=p})--当在newclass中找不到key对应的value时,就到p类中查找
function newclass:new(newobject)
newobject = newobject or {}
setmetatable(newobject,{__index = newclass})--当在newobject找不到key对应的value时,就到newclass类中查找
return newobject
end
return newclass
end
testA = classA:new()
print("testA ==> ",testA.className)
print("testA ==> ",testA.name) testB = inherit(classA):new({name = "testB"})
print("testB ==> ",testB.className)
print("testB ==> ",testB.name) testC = inherit(classA):new()
print("testC ==> ",testC.className)
print("testC ==> ",testC.name) testD = inherit(testB):new()
print("testD ==> ",testD.className)
print("testD ==> ",testD.name)

testA ==> classA
testA ==> classAInst
testB ==> classB
testB ==> testB
testC ==> classB
testC ==> classAInst
testD ==> classB
testD ==> testB

lua中使用table实现类和继承的更多相关文章

  1. lua中遍历table的几种方式比较

    当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: for key, value in pairs(tbtes ...

  2. Lua中使用table实现的其它5种数据结构

    Lua中使用table实现的其它5种数据结构 lua中的table不是一种简单的数据结构,它可以作为其他数据结构的基础,如:数组,记录,链表,队列等都可以用它来表示. 1.数组 在lua中,table ...

  3. 递归打印lua中的table

    在lua中,table是比较常用的数据形式,有时候为了打印出里面的内容,需要做一些特殊处理. 废话不多讲,直接粘代码: print = release_print -- 递归打印table local ...

  4. Lua学习笔记5:类及继承的实现

    -- Lua中类的实现 -------------------------------- 基类 ---------------------------- classBase = {x = 0,y = ...

  5. lua中的table、stack和registery

    ok,前面准备给一个dll写wrapper,写了篇日志,看似写的比较明白了,但是其实有很多米有弄明白的.比如PIL中使用的element,key,tname,field这些,还是比较容易混淆的.今天正 ...

  6. lua 中protobuf repeated 嵌套类 复合类型

    PB基础知识科普 syntax = "proto2"; package PB; message Item { required string name = ; } message ...

  7. Lua中的table函数库

    table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...

  8. lua中求table长度

    关于lua table介绍,看以前的文章http://www.cnblogs.com/youxin/p/3672467.html. 官方文档是这么描述#的: 取长度操作符写作一元操作 #. 字符串的长 ...

  9. Lua中的table构造式(table constructor)

    最简单的构造式就是一个空构造式{},用于创建一个空table. 构造式还可以用于初始化数组.例如,以下语句:days = {"Sunday", "Monday" ...

随机推荐

  1. 基础篇:6)形位公差标注(GD&T标准)-总章

    本章目的:理解GD&T概念,读懂和绘制GD&T图纸.本章是GD&T指引章节. 1.GD&T概念 GD&T  是 Geometric  Dimensioning ...

  2. vue,下级页面刷新导致路由跳转带过来的数据消失的解决方法

    if(typeof(this.$route.query.result)=='string'){ //刷新时走这 }else{ //正常路由跳转过来后就把数据塞到 localStorage let ob ...

  3. 使用Session监听器实现统计在线人数

    使用Session监听器实现统计在线人数 1.工作目录结构 包含监听器类和jsp页面 2.session监听器 首先利用session监听器来实现对访问网站时的session计数,当有session创 ...

  4. css :before 内容左边 分割线(四)

    商品 左边分割线,使用css伪类实现,before or  after <style> *{ margin:; padding:; } .clearfix { *zoom:; } .cle ...

  5. String字符串排序1.8 lamda表达式以及1.7自定义排序

    // 1.8 public class text { public static void main(String[] args) { String s1 = "哈哈哈11,呵呵呵22,嘿嘿 ...

  6. C#常用的引用

    1.使用ConfigurationManager需要在.net引用中添加System.Configuration引用 2.使用HttpContext需要在.net引用中添加System.Web引用

  7. Eclipse的简单的用法大全

    Eclipse我认为最重要的功能:断点调试 Debug的作用: 调试程序并且查看程序的执行流程 如何查看程序执行的流程 断点(就是一个标记,表示从哪里开始) 设置断点(在你想要断点的代码的左边双击即可 ...

  8. oracle dump的使用心得

    使用DS开发的时候,有的时候会遇到一个问题:数据库层面定义的空格与DS自已定义的空格概念不一致,导致生成的数据会有一定的问题. 举例来说: 在数据库里面定义CHAR(20),如果插入的字符不足20的时 ...

  9. 【Lua】Lua + LWT + ExtJS构建目录树

    Lua处理后台逻辑,Lua lwt搭建后台程序,ExtJS根据后台传来的json数据构建目录树. 前台html和ExtJS代码不用多讲,直接上代码: treePanel.html <html&g ...

  10. python 对象/变量&赋值的几点思考

    python 对象/变量 对象 Every object has an identity, a type and a value. An object's identity never changes ...