lua no class

It is a prototype based language。

在此语言中没有class关键字来创建类。

现代ES6, 已经添加class类。 prototype based 语言没啥优势。

lua 如何构建class机制?

https://github.com/fanqingsong/oopclass.lua

提供lua的 Object Oriented Programing Class 实现: 比其他实现更加轻量 https://github.com/Yonaba/Lua-Class-System 特色功能: 1、 虚类声明 2、 单例声明 3、 类冰封, 一旦冰封后, 类的属性不能被改变。

code -- oopclass.lua

local _M = {}

-- Instantiates a class
local function _instantiate(class, ...)
-- 抽象类不能实例化
if rawget(class, "__abstract") then
error("asbtract class cannot be instantiated.")
end -- 单例模式,如果实例已经生成,则直接返回
if rawget(class, "__singleton") then
-- _G[class]值为本class的实例
if _G[class] then
return _G[class]
end
end local inst = setmetatable({__class=class}, {__index=class})
if inst.__init__ then
inst:__init__(...)
end --单例模式,如果实例未生成,则将实例记录到类中
if rawget(class, "__singleton") then
if not _G[class] then
_G[class] = inst -- 对类对象增加实例获取接口
class.getInstance = function ( self )
return _G[class]
end -- 销毁单例,为后续建立新单例准备
class.destroyInstance = function ( self )
_G[class] = nil
end
end
end return inst
end -- LUA类构造函数
function _M.class(base)
local metatable = {
__call = _instantiate,
} -- 先查原型表,然后查父亲类
metatable.__index=function(t, k)
local v = t.__prototype[k]
if v then
return v
end local parent = t.__parent
if parent then
return parent[k]
end return nil
end -- 缓存类的field
metatable.__newindex=function (t,k,v)
rawset(t.__prototype, k, v)
end local _class = {}
-- __parent 属性缓存父类
_class.__parent = base or {}
-- 存储此类的所有field
_class.__prototype = {} -- 在class对象中记录 metatable ,以便重载 metatable.__index
_class.__metatable = metatable -- 将类冷冻,不允许新建删除修改
_class.freeze = function ( self )
local mt = getmetatable(self) mt.__newindex=function (t,k,v)
error("class is frozen, cannot revise")
end
end return setmetatable(_class, metatable)
end --- Test whether the given object is an instance of the given class.
-- @param object Object instance
-- @param class Class object to test against
-- @return Boolean indicating whether the object is an instance
-- @see class
-- @see clone
function _M.instanceof(object, class)
local objClass = object.__class
if not objClass then
return false
end while objClass do
if objClass == class then
return true
end
objClass = objClass.__parent
end return false
end return _M

使用

local oopclass = require("oopclass")

local class = oopclass.class
local instanceof = oopclass.instanceof local superTab = class()
superTab.test = function ( self )
print("superTab test")
end superTab:freeze() superTab.test2 = function ( self )
print("superTab test2")
end local tab = class(superTab) local tabObj = tab()
tabObj:test() print( instanceof(tabObj, tab) ) print( instanceof(tabObj, superTab) )

lua 面向对象编程类机制实现的更多相关文章

  1. Lua面向对象编程

    Lua中的table就是一种对象,看以下一段简单的代码: , b = } , b = } local tb3 = tb1 if tb1 == tb2 then print("tb1 == t ...

  2. Python面向对象编程 -- 类和实例、访问限制

    面向对象编程 Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程 ...

  3. 13_Python的面向对象编程-类class,对象object,实例instance

    1.面向对象概述 1.类是用来描述对象的工具,把拥有相同属性和行为的对象分为一组     2.对象是由类实例化出来的一个具体的对象         属性: 对象拥有的名词,用变量表示         ...

  4. Python记录14:面向对象编程 类和对象

    '''现在主流的编程思想有两种,一种是面向对象,一种是面向过程面向过程编程 核心是过程二字,过程指的是解决问题的步骤,即先干什么.再干什么.最后干什么... 基于该思想编写程序就好比再设计一条流水线, ...

  5. Python 面向对象编程——类定义与对象

    <类定义与对象声明> 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对 ...

  6. CSIC_716_20191125【面向对象编程--类以及类的实例化】

    面向对象编程:是一种编程思想 对象的定义:特征与功能的集合体 优点:可扩展性强 缺点:编程复杂度高,难度偏大 类的定义:一系列对象之间相同特征与技能的结合体 调用类的时候(实例化是时候),发生的事情: ...

  7. Python实用笔记 (18)面向对象编程——类和实例

    类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各 ...

  8. python -- 面向对象编程(类、对象)

    一.类 类是用来描述具有相同的属性和方法的对象的集合. 它定义了该集合中每个对象共同拥有的属性和方法. 类是一个独立的单位,它有一个类名,其内部包括成员变量和成员方法,分别用于描述对象的属性和行为. ...

  9. lua面向对象编程 《lua程序设计》 16章 笔记

    Lua中的table就是一种对象,即它拥有状态.拥有独立于其值的标识(self).table与对象一样具有独立于创建者和创建地的征集周期 什么叫对象拥有独立的生命周期? Account = {bala ...

随机推荐

  1. 关于APP接口设计(转)

    最近一段时间一直在做APP接口,总结一下APP接口开发过程中的注意事项: 1.效率:接口访问速度 APP有别于WEB服务,对服务器端要求是比较严格的,在移动端有限的带宽条件下,要求接口响应速度要快,所 ...

  2. react native TextInput

    今天我想说一下react native中的一个控件,TextInput 翻译过来就是文本输入,对应着android中的EditText.我们先看一下官方是怎样描述的.TextInput是一个允许用户在 ...

  3. psql-10权限和事务

    用户和角色 用户和角色在整个数据库实例中都是全局的;

  4. 1.0 Quartz 2D 简介

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”   Quartz2D须知:   (1)Quartz 2D是苹果官方的二维绘图引擎,同时支持 ...

  5. 如何预览将要上传的图片-使用H5的FileAPI

    这篇将要说的东西已经不新鲜了. 参考资料: Reading files in JavaScript using the File APIs (鉴于作者在美国, 我姑且认为作者母语是英语, 当然链接中有 ...

  6. ZeroMQ接口函数之 :zmq_close - 关闭ZMQ socket

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_close zmq_close(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_close  ...

  7. 阿里云分布式关系数据库DRDS笔记

    1.Join左边的表查询数据越少,性能越好 2.广播表作为Join的驱动表 3.SQL的Limit优化 SELECT * FROM t_order o WHERE o.id IN ( SELECT i ...

  8. About_PHP_验证码的生成

    验证码就是一张图片,用到几个关键字: <?php session_start(); $arr = array( 'a','b','c','d','e','f','g','h','i','j',' ...

  9. HDU1010 DFS+剪枝

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. Learn ZYNQ (7)

    矩阵相乘的例子 参考博客:http://blog.csdn.net/kkk584520/article/details/18812321 MatrixMultiply.c typedef int da ...