Lua中面向对象
一、Lua中类的简单实现:
(1)版本——摘自 Cocos2.0中的:
--Create an class.
function class(classname, super)
local superType = type(super)
local cls if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end if superType == "function" or (super and super.__ctype == ) then
-- inherited from native C++ Object
cls = {} if superType == "table" then
-- copy fields from super
for k,v in pairs(super) do cls[k] = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
end cls.ctor = function() end
cls.__cname = classname
cls.__ctype = function cls.new(...)
local instance = cls.__create(...)
-- copy fields from class to native object
for k,v in pairs(cls) do instance[k] = v end
instance.class = cls
instance:ctor(...)
return instance
end else
-- inherited from Lua Object
if super then
cls = clone(super)
cls.super = super
else
cls = {ctor = function() end}
end cls.__cname = classname
cls.__ctype = -- lua
cls.__index = cls function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end return cls
end
下面是测试这段功能的代码片:
----------------------------------create a father-------------------------------------------------
local testClassbase = class("testClassbase")
function testClassbase:ctor(data,closeUI)
print(data)
self.a = data
print(self.a)
self.b = closeUI
end function testClassbase:print_member()
print("-----data")
print(self.a)
print("===closeUI")
print(self.b)
end
---------------------------------main---------------------------------------------------------------------
print("++++begin++++")
testa =testClassbase.new(,true)
testa:print_member() --注意这里要用冒号去调用
如果要构造一个派生类来继承上面那个基类,那么需要加上下面这么一段
function clone(object) --clone函数
local lookup_table = {} --新建table用于记录
local function _copy(object) --_copy(object)函数用于实现复制
if type(object) ~= "table" then
return object ---如果内容不是table 直接返回object(例如如果是数字\字符串直接返回该数字\该字符串)
elseif lookup_table[object] then
return lookup_table[object] --这里是用于递归滴时候的,如果这个table已经复制过了,就直接返回
end
local new_table = {}
lookup_table[object] = new_table --新建new_table记录需要复制的二级子表,并放到lookup_table[object]中.
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value) --遍历object和递归_copy(value)把每一个表中的数据都复制出来
end
return setmetatable(new_table, getmetatable(object))--每一次完成遍历后,就对指定table设置metatable键值
end
return _copy(object) --返回clone出来的object表指针/地址
end
测试继承的代码:
--------------------------------copy a child-----------------------------------------------------------
local testClassChild = class("testClassChild",testClassbase) function testClassChild:ctor(data,UIclose)
self.a = data
self.b = UIclose
end function testClassChild:Greet()
self.sayhello()
self:print_member()
end ---------------------------------main---------------------------------------------------------------------
print("++++begin++++")
testa =testClassbase.new(,true)
testa:print_member() --注意这里要用冒号去调用 print("--------------------------")
testb = testClassChild.new(,false)
testb:Greet()
完整代码:
--Create an class.
function class(classname, super)
local superType = type(super)
local cls if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end if superType == "function" or (super and super.__ctype == ) then
-- inherited from native C++ Object
cls = {} if superType == "table" then
-- copy fields from super
for k,v in pairs(super) do cls[k] = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
end cls.ctor = function() end
cls.__cname = classname
cls.__ctype = function cls.new(...) --
local instance = cls.__create(...)
-- copy fields from class to native object
for k,v in pairs(cls) do instance[k] = v end
instance.class = cls
instance:ctor(...)
return instance
end else
-- inherited from Lua Object
if super then
cls = clone(super)
cls.super = super
else
cls = {ctor = function() end}
end cls.__cname = classname
cls.__ctype = -- lua
cls.__index = cls function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end return cls
end --------------------------------------------------clone_function-------------------------------------------------------------------------------- function clone(object) --clone函数
local lookup_table = {} --新建table用于记录
local function _copy(object) --_copy(object)函数用于实现复制
if type(object) ~= "table" then
return object ---如果内容不是table 直接返回object(例如如果是数字\字符串直接返回该数字\该字符串)
elseif lookup_table[object] then
return lookup_table[object] --这里是用于递归滴时候的,如果这个table已经复制过了,就直接返回
end
local new_table = {}
lookup_table[object] = new_table --新建new_table记录需要复制的二级子表,并放到lookup_table[object]中.
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value) --遍历object和递归_copy(value)把每一个表中的数据都复制出来
end
return setmetatable(new_table, getmetatable(object))--每一次完成遍历后,就对指定table设置metatable键值
end
return _copy(object) --返回clone出来的object表指针/地址
end ----------------------------------create a father-------------------------------------------------
local testClassbase = class("testClassbase")
function testClassbase:ctor(data,closeUI)
-- print(data)
self.a = data
-- print(self.a)
self.b = closeUI
end function testClassbase:sayhello()
print("father say hello")
end function testClassbase:print_member()
print("-----data")
print(self.a)
print("===closeUI")
print(self.b)
end --------------------------------copy a child-----------------------------------------------------------
local testClassChild = class("testClassChild",testClassbase) function testClassChild:ctor(data,UIclose)
self.a = data
self.b = UIclose
end function testClassChild:Greet()
self.sayhello()
self:print_member()
end ---------------------------------main---------------------------------------------------------------------
print("++++begin++++")
testa =testClassbase.new(,true)
testa:print_member() --注意这里要用冒号去调用 print("--------------------------")
testb = testClassChild.new(,false)
testb:Greet()
(2) 版本:
据说是云风大神写的:
这个是构造class的函数和上面的构造方法有很大的不同:
具体讲解我找了一篇写的还不错的博客:http://blog.csdn.net/mywcyfl/article/details/37706085
下面代码可以直接拿来运行:
local _class={}
function class(super)
local class_type={}
class_type.ctor = false
class_type.super = super
class_type.new =
function(...)
local obj={}
do
local create
create = function(c,...)
if c.super then
create(c.super,...)
end
if c.ctor then
c.ctor(obj,...)
end
end
create(class_type,...)
end
setmetatable(obj,{ __index = _class[class_type] })
return obj
end
local vtbl={}
_class[class_type]=vtbl
setmetatable(class_type,{__newindex=
function(t,k,v)
vtbl[k]=v
end
})
if super then
setmetatable(vtbl,{__index=
function(t,k)
local ret=_class[super][k]
vtbl[k]=ret
return ret
end
})
end
return class_type
end
测试代码:
base_type=class() -- 定义一个基类 base_type
function base_type:ctor(x) -- 定义 base_type 的构造函数
print("base_type ctor")
self.x=x
end function base_type:print_x() -- 定义一个成员函数 base_type:print_x
print(self.x)
end
function base_type:hello() -- 定义另一个成员函数 base_type:hello
print("hello base_type")
end test=class(base_type) -- 定义一个类 test 继承于 base_type
function test:ctor() -- 定义 test 的构造函数
print("test ctor")
end
function test:hello() -- 重载 base_type:hello 为 test:hello
--test.super:hello()
print("hello test")
end a=test.new() -- 输出两行,base_type ctor 和 test ctor 。这个对象被正确的构造了。
a:print_x() -- 输出 ,这个是基类 base_type 中的成员函数。
a:hello() -- 输出 hello test ,这个函数被重载了。
Lua中面向对象的更多相关文章
- Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类
主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3 参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 在 ...
- lua 中的面向对象
lua 是一种脚步语言,语言本身并不具备面向对象的特性. 但是我们依然可以利用语言的特性,模拟出面向对象的特性. 面向对象的特性通常会具备:封装,继承,多态的特性,如何在lua中实现这些特性,最主要的 ...
- Cocos2d-x 脚本语言Lua中的面向对象
Cocos2d-x 脚本语言Lua中的面向对象 面向对象不是针对某一门语言,而是一种思想.在面向过程的语言也能够使用面向对象的思想来进行编程. 在Lua中,并没有面向对象的概念存在,没有类的定义和子类 ...
- lua中的面向对象编程
简单说说Lua中的面向对象 Lua中的table就是一种对象,看以下一段简单的代码: 上述代码会输出tb1 ~= tb2.说明两个具有相同值得对象是两个不同的对象,同时在Lua中table是引用类型的 ...
- 【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
一.简介 Lua是一门非常强大.非常灵活的脚本语言,自它从发明以来,无数的游戏使用了Lua作为开发语言.但是作为一款脚本语言,Lua也有着自己的不足,那就是它本身并没有提供面向对象的特性,而游戏开发是 ...
- 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
[游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态 阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...
- Lua中的面向对象编程详解
简单说说Lua中的面向对象 Lua中的table就是一种对象,看以下一段简单的代码: 复制代码代码如下: local tb1 = {a = 1, b = 2}local tb2 = {a = 1, b ...
- Lua 之面向对象编程
Lua 之面向对象编程 Lua并不是为面向对象而设计的一种语言,因此,仅从原生态语法上并不直接支持面向对象编程,但Lua的设计中仍然包含了很多面向对象的思想,理解它们也更有助于理解Lua自身的一些高级 ...
- [译] Closures in Lua - Lua中的闭包
原文:(PDF) . 摘要 一等(first-class)函数是一种非常强大的语言结构,并且是函数式语言的基础特性.少数过程式语言由于其基于栈的实现,也支持一等函数.本文讨论了Lua 5.x用于实现一 ...
随机推荐
- python -django 之第三方支付
神魔是第三方支付: 第三方支付是指具有一定实力和信誉保障的第三方独立机构.通过与各大银行签订合同,建立连接用户和银行支付结算系统的平台,从而实现电子支付模式.从另一个角度来看,第三方支付就是非金融机构 ...
- alpha冲刺(2/10)
前言 队名:旅法师 作业链接 队长博客 燃尽图 会议 会议照片 会议内容 陈晓彬(组长) 今日进展: 召开会议 安排任务 博客撰写 构建之法的阅读 问题困扰: 分配任务,还是不熟练,对后台不熟悉,不知 ...
- HDU5542 The Battle of Chibi
题意 给出长度为n的序列,问这个序列中有多少个长度为m的单调递增子序列. \(1\le M\le N\le 1000\) 分析 用F[i,j]表示前j个数构成以Aj为结尾的数列中,长度为i的严格递增子 ...
- 【SpringBoot】搜索框架ElasticSearch介绍和整合SpringBoot
========================12章 搜索框架ElasticSearch介绍和整合SpringBoot ============================= 加入小D课堂技术交 ...
- How to Create an PostgreSQL Extension
转自:https://severalnines.com/blog/creating-new-modules-using-postgresql-create-extension Extensibilit ...
- TableLayoutPanel 动态添加 行 列
//添加行 横排 ++this.tbPnl.RowCount; this.tbPnl.RowStyles.Add(new System.Windows.Forms.RowStyle(System ...
- Nginx网站实现ssl安全套接字
nginx.conf配置 server { listen 443 ssl; server_name www.example.com; ssl on; ssl_certificate cert.pem; ...
- 18.2 of的函数集中的of是Open Firmware的缩写
内核中操作dtb的一套函数都是of开头,这个of是open firmware.dts的方法来源于open Firmware On Sun SPARC systems, the Open Firmwar ...
- windows 安装lua-5.3.4 --引用自https://blog.csdn.net/wangtong01/article/details/78296369
版权声明:本文为博主原创文章,转载时请标明出处.http://blog.csdn.net/wangtong01 https://blog.csdn.net/wangtong01/article/det ...
- Flask 框架
装饰器知识回顾 http://www.cnblogs.com/0bug/p/7978595.html 普通装饰器格式: def wrapper(func): def inner(*args, **kw ...