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" ...
随机推荐
- next_permutation(全排列)
废话不多说,直接上代码,谁测试,谁知道 C++: #include<bits/stdc++.h> using namespace std; typedef long long ll; in ...
- 剑指offer——面试题16:数值的整数次方
// 面试题16:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需 ...
- Oracle触发器简单使用记录
在ORACLE系统里,触发器类似函数和过程.1.触发器类型:(一般为:语句级触发器和行级触发器.) 1).DML触发器: 创建在表上,由DML事件引发 2).instead of触发器: 创建在视图上 ...
- ORC Files
ORC 全称是Optimized Row Columnar,意思是优化的RC file,优化行列式. ORC 文件格式提供了一个很高效的方式来存储hive数据.它旨在克服其他hive文件格式的限制.当 ...
- Json 装 list<object>objectList
List<MallGoodComment> mallGoodCommentList = JSONObject.parseArray(mallGoodComments, MallGoodCo ...
- 3dsmax2017卸载/安装失败/如何彻底卸载清除干净3dsmax2017注册表和文件的方法
3dsmax2017提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dsmax2017失败提示3dsmax2017安装未完成,某些产品无法安装,也有时候想重新 ...
- linux下对应mysql数据库的常用操作
ssh管理工具连接mysql数据库. 一.连接mysql数据库: 通过shh管理工具,登录linux的用户名,密码,进入ssh的命令行界面后,执行如下命令: mysql -u 数据库用户名 -p 然后 ...
- ugui的优化
参考文章 https://www.jianshu.com/p/061e67308e5f https://www.jianshu.com/p/8a9ccf34860e http://blog.jobbo ...
- Cucumber capybara 每个Scenario登陆一次
hook.rb中添加: After do |scenario| Capybara.current_session.instance_variable_set(:@touched, false)end ...
- poj 2954 Triangle
pick公式+gcd公式 #include<iostream> #include<map> #include<string> #include<cstring ...