一、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中面向对象的更多相关文章

  1. Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 在 ...

  2. lua 中的面向对象

    lua 是一种脚步语言,语言本身并不具备面向对象的特性. 但是我们依然可以利用语言的特性,模拟出面向对象的特性. 面向对象的特性通常会具备:封装,继承,多态的特性,如何在lua中实现这些特性,最主要的 ...

  3. Cocos2d-x 脚本语言Lua中的面向对象

    Cocos2d-x 脚本语言Lua中的面向对象 面向对象不是针对某一门语言,而是一种思想.在面向过程的语言也能够使用面向对象的思想来进行编程. 在Lua中,并没有面向对象的概念存在,没有类的定义和子类 ...

  4. lua中的面向对象编程

    简单说说Lua中的面向对象 Lua中的table就是一种对象,看以下一段简单的代码: 上述代码会输出tb1 ~= tb2.说明两个具有相同值得对象是两个不同的对象,同时在Lua中table是引用类型的 ...

  5. 【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态

    一.简介 Lua是一门非常强大.非常灵活的脚本语言,自它从发明以来,无数的游戏使用了Lua作为开发语言.但是作为一款脚本语言,Lua也有着自己的不足,那就是它本身并没有提供面向对象的特性,而游戏开发是 ...

  6. 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态

    [游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态   阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...

  7. Lua中的面向对象编程详解

    简单说说Lua中的面向对象 Lua中的table就是一种对象,看以下一段简单的代码: 复制代码代码如下: local tb1 = {a = 1, b = 2}local tb2 = {a = 1, b ...

  8. Lua 之面向对象编程

    Lua 之面向对象编程 Lua并不是为面向对象而设计的一种语言,因此,仅从原生态语法上并不直接支持面向对象编程,但Lua的设计中仍然包含了很多面向对象的思想,理解它们也更有助于理解Lua自身的一些高级 ...

  9. [译] Closures in Lua - Lua中的闭包

    原文:(PDF) . 摘要 一等(first-class)函数是一种非常强大的语言结构,并且是函数式语言的基础特性.少数过程式语言由于其基于栈的实现,也支持一等函数.本文讨论了Lua 5.x用于实现一 ...

随机推荐

  1. Tomcat7.0/8.0 详细安装配置图解,以及UTF-8编码配置

    Tomcat7.0/8.0 详细安装配置图解,以及UTF-8编码配置 2017年01月24日 10:01:48 阅读数:51265 标签: tomcattomcat安装tomcat配置tomcat编码 ...

  2. C语言求行列式的值

    #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <window ...

  3. 《Linux内核原理与分析》第八周作业

    课本:第七章 可执行程序工作原理 ELF目标文件格式 目标文件:编译器生成的文件. 目标文件的格式:out格式.COFF格式.PE(windows)格式.ELF(Linux)格式. ELF(Execu ...

  4. 互联网同步yum服务器阿里云 reposync createrepo

    参考文章: https://www.cnblogs.com/lldsn/p/10479493.html 系统版本centos 7.5 最小化安装 修改主机名 hostnamectl set-hostn ...

  5. Linux(CentOS 7.0)安装Oracle11g R2

    // 注释 # root用户 $oracle用户 1. 关闭安全措施    # chkconfig iptables off // 永久关闭防火墙 # serviceiptables stop // ...

  6. windows7 64位安装tensorflow 1.4.0 CPU版本

    机器学习和深度学习真是新生代的宠儿,我也被安排来搞这个了,这下是真的从0开始了.看了几天ppt,想跑跑代码试试,装个环境. 都说tensorflow很火很好用,反正我什么也不懂,准备把这些框架一个一个 ...

  7. 删除文件夹下各级子目录中的.svn文件

    建立一个文本文件,取名为removeSvn.reg(扩展名由txt改为reg),内容如下 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHIN ...

  8. 谷歌浏览器内核Cef js代码整理(一)

    尊重作者原创,未经作者允许不得转载!作者:xtfnpgy,原文地址: https://www.cnblogs.com/xtfnpgy/p/9285359.html 一.js基础知识 <!--   ...

  9. List 的add()与addAll()的区别

    add 是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素addAll 是传入一个List,将此List中的所有元素加入到当前List中,也就 ...

  10. 分布式之redis复习精讲

    看到一片不错的精简的redis文档,转载之,便于复习梳理之用 转自:https://www.cnblogs.com/rjzheng/p/9096228.html ------------------- ...