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用于实现一 ...
随机推荐
- mlsql 基本操作
数据库的操作: 1.创建 create databases python_test_01(库名,自定义)chaeset = utf8; 2.删除 drop database python_test_0 ...
- lucas 模板 洛古模板题
#include<bits/stdc++.h> #define int long long using namespace std; ; int a[maxn]; int quick(in ...
- python pytz时区设置模块
如果你的程序要考虑时区,可以使用pytz.pytz官方文档:http://pytz.sourceforge.net/我使用的python版本:3.7.1 datetime模块中有tzinfo相关的东西 ...
- golang 写日志到syslog
应用程序可以通过 UNIX domain sockets, UDP or TCP,向syslog守护进程发送日志.syslog守护进程可以在远端. 这样,就可以不用单独收集应用程序的日志了. gola ...
- 量化分析v1
量化分析v1 # -*- coding: utf-8 -*- """ Created on Wed Apr 11 10:13:32 2018 @author: chens ...
- python3学习笔记七(字典)
参照http://www.runoob.com/python3/python3-dictionary.html 字典 字典是另一种可变容器模型,且可以存储任意类型对象. dict1 = {key1:v ...
- [转][C#]压缩解压
{ internal static class Compressor { public static Stream Decompress(Stream source, bool bidiStream) ...
- Hibernate一对多OnetoMany
------------------------Hibernate一对多OnetoMany 要点: 配置在一端. 1.如果是单向关联,即只在一端配置OneToMany,多端不配置ManyToOne.则 ...
- 202. 阿里Pandora Boot
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...
- Dubbo源码分析
Dubbo源码分析1 Dubbo源码分析2 dubbo源码阅读:rpc请求处理流程(1) 架构设计:系统间通信(17)——服务治理与Dubbo 中篇(分析) 13. Dubbo原理解析-注册中心之Zo ...