lua中使用table实现类和继承
--因为只有当读写不存在的域时,才会触发__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实现类和继承的更多相关文章
- lua中遍历table的几种方式比较
当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: for key, value in pairs(tbtes ...
- Lua中使用table实现的其它5种数据结构
Lua中使用table实现的其它5种数据结构 lua中的table不是一种简单的数据结构,它可以作为其他数据结构的基础,如:数组,记录,链表,队列等都可以用它来表示. 1.数组 在lua中,table ...
- 递归打印lua中的table
在lua中,table是比较常用的数据形式,有时候为了打印出里面的内容,需要做一些特殊处理. 废话不多讲,直接粘代码: print = release_print -- 递归打印table local ...
- Lua学习笔记5:类及继承的实现
-- Lua中类的实现 -------------------------------- 基类 ---------------------------- classBase = {x = 0,y = ...
- lua中的table、stack和registery
ok,前面准备给一个dll写wrapper,写了篇日志,看似写的比较明白了,但是其实有很多米有弄明白的.比如PIL中使用的element,key,tname,field这些,还是比较容易混淆的.今天正 ...
- lua 中protobuf repeated 嵌套类 复合类型
PB基础知识科普 syntax = "proto2"; package PB; message Item { required string name = ; } message ...
- Lua中的table函数库
table.concat(table, sep, start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...
- lua中求table长度
关于lua table介绍,看以前的文章http://www.cnblogs.com/youxin/p/3672467.html. 官方文档是这么描述#的: 取长度操作符写作一元操作 #. 字符串的长 ...
- Lua中的table构造式(table constructor)
最简单的构造式就是一个空构造式{},用于创建一个空table. 构造式还可以用于初始化数组.例如,以下语句:days = {"Sunday", "Monday" ...
随机推荐
- 128th LeetCode Weekly Contest Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- Js写九宫格抽奖
国庆出去转了一圈,回来及时把以前写的一些有用的在这儿记录一下 --------------------------------------------我是分割线-------------------- ...
- SpringCloud---客户端负载均衡---Spring Cloud Ribbon
1.概述 1.1 Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具: 基于Netflix Ribbon实现: 通过Spring Cloud的封装,可以轻松将面向服务 ...
- 1.python学习计划
1.python学习 第一次使用博客园作为学习记录日志,希望能在这里记录自己的学习点滴. 慢慢去挖掘它的强大功能吧
- openerp学习笔记 模块结构分析
以OpenERP7.0中的 hr_expense 模块为例: 如图中代码所示: __init__.py :和普通 Python 模块中的__init__.py 作用相同,主要用于引用模块根目录下的.p ...
- Asp.Net webconfig中使用configSections的用法
最近闲来无事,研究研究公司的框架,无意中打开了webconfig页面,发现了一个我不认识的节点<configSections></configSections>,于是百度之,大 ...
- 在W3C SCHOOL网站上发现一个关于Schema的错误
原地址是http://www.w3school.com.cn/schema/schema_complex_empty.asp 下面这个例子是不正确的 xmlspy报错. 因为<xs:restri ...
- Android控件之ListView的使用
ListView是Android当中一个非常常用的数据显示控件. 第一种可以使用List<HashMap<String , Object>>,作为适配器的数据源来显示要显示的数 ...
- easyui导出当前datagrid数据(Word)
JS代码可参考http://www.cnblogs.com/mu1516633121/p/7753423.html 同样是winform架构下应用到Aspose.Words来读写Word文档 其中Se ...
- Java基础(10)——小结与填坑
前面都写了9篇啦,虽然断断续续发了半个月,写着写着会发现每篇中都有些比较重要的地方没有讲到~这篇还是需要填一填目前我已发现的坑了~ 一. 小结 Java编译命令 javac.运行命令java java ...