lua 面向对象编程类机制实现
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 面向对象编程类机制实现的更多相关文章
- Lua面向对象编程
Lua中的table就是一种对象,看以下一段简单的代码: , b = } , b = } local tb3 = tb1 if tb1 == tb2 then print("tb1 == t ...
- Python面向对象编程 -- 类和实例、访问限制
面向对象编程 Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程 ...
- 13_Python的面向对象编程-类class,对象object,实例instance
1.面向对象概述 1.类是用来描述对象的工具,把拥有相同属性和行为的对象分为一组 2.对象是由类实例化出来的一个具体的对象 属性: 对象拥有的名词,用变量表示 ...
- Python记录14:面向对象编程 类和对象
'''现在主流的编程思想有两种,一种是面向对象,一种是面向过程面向过程编程 核心是过程二字,过程指的是解决问题的步骤,即先干什么.再干什么.最后干什么... 基于该思想编写程序就好比再设计一条流水线, ...
- Python 面向对象编程——类定义与对象
<类定义与对象声明> 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对 ...
- CSIC_716_20191125【面向对象编程--类以及类的实例化】
面向对象编程:是一种编程思想 对象的定义:特征与功能的集合体 优点:可扩展性强 缺点:编程复杂度高,难度偏大 类的定义:一系列对象之间相同特征与技能的结合体 调用类的时候(实例化是时候),发生的事情: ...
- Python实用笔记 (18)面向对象编程——类和实例
类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各 ...
- python -- 面向对象编程(类、对象)
一.类 类是用来描述具有相同的属性和方法的对象的集合. 它定义了该集合中每个对象共同拥有的属性和方法. 类是一个独立的单位,它有一个类名,其内部包括成员变量和成员方法,分别用于描述对象的属性和行为. ...
- lua面向对象编程 《lua程序设计》 16章 笔记
Lua中的table就是一种对象,即它拥有状态.拥有独立于其值的标识(self).table与对象一样具有独立于创建者和创建地的征集周期 什么叫对象拥有独立的生命周期? Account = {bala ...
随机推荐
- [转载]memcached完全剖析--1. memcached的基础
转载自:http://charlee.li/memcached-001.html 翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西很有用,希望大家喜欢. 发表日:200 ...
- Borland C++建立工程菜鸟教程
实在不敢相信,学校里现在教学用的编译器还是七八十年代的老古董,难道这东西更能培养从没接触过代码的学生深刻理解c语言的运行流程,不得不说像VisualStdio这样的重量级开发环境确实屏蔽了很多底层的运 ...
- LeetCode——Best Time to Buy and Sell Stock I (股票买卖时机问题1)
问题: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- T-SQL 基础学习 04
索引 示意图 定义 索引提供指针指向存储在表中指定列的数据值,然后根据指定的排序次序排列这些指针 作用 通过使用索引,大大提高数据库的检索速度,改善数据库性能 索引六大类 1. ...
- Fzu2124 - 吃豆人 BFS
Description 吃豆人是一款非常经典的游戏,游戏中玩家控制吃豆人在地图上吃光所有豆子,并且避免被怪物抓住. 这道题没有怪物,将游戏的画面分成n*m的格子,每格地形可能为空地或者障碍物,吃豆人可 ...
- C# 词法分析器(五)转换 DFA
系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 在上一篇文章中,已经得到了与正则表达式等价的 NFA ...
- win8 系统安装node环境记录
原先我是用win7环境安装node很方便,到了win8系统突然变了,让我顿时困惑了一段时间,但还是被我找到方式解决了,记录一下解决方案: 首先在网上看了一些资料说win8下安装node环境会出错,但我 ...
- 【Cocos2d-x游戏开发】Cocos2d-x中的弱联网技术
在上一篇博客中,我们一起学习了如何在Cocos2d-x中存储数据和读取信息,本篇博客我们将一起讨论和数据存储同样重要的联网技术. 一.弱联网技术介绍 在网络游戏中许多重要的功能都需要网络连接,而根据需 ...
- JavaScript中的this指向
this是谁 技术一般水平有限,有什么错的地方,望大家指正. this代指当前对象super调用父类的构造函数,应表会运网数物,加载驱动建立链接执行SQL处理结果,直到现在每想起这三点就能想起我上大学 ...
- css 一些灵动性的小方法
CSS: 1.当鼠标放到一个图片上的时候,他会给你显示一些图片的信息或者是一些其他的信息. <!DOCTYPE html> <html lang="en"> ...