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 ...
随机推荐
- Mac下环境变量配置
Mac下的常用环境变量配置文件 1./etc/profile (建议不修改这个文件 ) 全局(公有)配置,不管是哪个用户,登录时都会读取该文件. 2./etc/bashrc (一般在这个文件 ...
- Android-- ImageLoader-- UIL doesn't support scheme(protocol) by default [pg].
在ImageLoader加载图片是, 地址是容易出错的,特别是本地图片: String imageUri = "http://site.com/image.png"; // fro ...
- java多线程面试题
很多核心Java面试题来源于多线程(Multi-Threading)和集合框架(Collections Framework),理解核心线程概念时,娴熟的实际经验是必需的.这篇文章收集了Java线程方面 ...
- mac os x 10.10.3 安装protoc
预装如下环境 autoconf 2.6.9automake 1.14libtool 2.4 Building from source Download latest version of procbu ...
- 怎样看懂Oracle的执行计划
怎样看懂Oracle的执行计划 一.什么是执行计划 An explain plan is a representation of the access path that is taken when ...
- 【原】iOS学习之UIStoryboardSegue解析
在 Storyboard 的可视化编程中,跳转界面就是按住 Ctrl 使用鼠标头一条连线就可以解决,相当的简单!本篇博客主要就是介绍这条连线,在iOS中,这条连线也是一个对象,也有其自己的初始化方法和 ...
- Web设计师值得收藏的10个jQuery特效
jQuery已经不是什么新鲜的事儿,以前总把它认为是非常难的东西,也就没有认真去了解他了.直到学完CSS的大部分内容,才开始接触这种"write less, do more" 的J ...
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- mongoVUE的增删改查操作使用说明
mongoVUE的增删改查操作使用说明 一. 查询 1. 精确查询 1)右键点击集合名,再左键点击Find 或者直接点击工具栏上的Find 2)查询界面,包括四个区域 {Find}区,查询条件格式{& ...
- Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...