lua原生不支持OOP特性

确实如此, 同时可以采用其它lua代码的方式实现OOP的特性。

OOP四大特性

抽象

封装

继承

多态

http://www.cnblogs.com/xiaosongluffy/p/5072501.html

四大基本特性:

抽象:提取现实世界中某事物的关键特性,为该事物构建模型的过程。对同一事物在不同的需求下,需要提取的特性可能不一样。得到的抽象模型中一般包含:属性(数据)和操作(行为)。这个抽象模型我们称之为。对类进行实例化得到对象。

封装:封装可以使类具有独立性和隔离性;保证类的高内聚。只暴露给类外部或者子类必须的属性和操作。类封装的实现依赖类的修饰符(public、protected和private等)

继承:对现有类的一种复用机制。一个类如果继承现有的类,则这个类将拥有被继承类的所有非私有特性(属性和操作)。这里指的继承包含:类的继承和接口的实现。

多态:多态是在继承的基础上实现的。多态的三个要素:继承、重写和父类引用指向子类对象。父类引用指向不同的子类对象时,调用相同的方法,呈现出不同的行为;就是类多态特性。多态可以分成编译时多态和运行时多态。

Code

--

-- Class helper routines

--

-- Instantiates a class

local function _instantiate(class, ...)

local inst = setmetatable({}, {__index = class})

if inst.__init__ then

inst:__init__(...)

end

return inst

end

--- Create a Class object (Python-style object model).

-- The class object can be instantiated by calling itself.

-- Any class functions or shared parameters can be attached to this object.

-- Attaching a table to the class object makes this table shared between

-- all instances of this class. For object parameters use the __init__ function.

-- Classes can inherit member functions and values from a base class.

-- Class can be instantiated by calling them. All parameters will be passed

-- to the __init__ function of this class - if such a function exists.

-- The __init__ function must be used to set any object parameters that are not shared

-- with other objects of this class. Any return values will be ignored.

-- @param base The base class to inherit from (optional)

-- @return A class object

-- @see instanceof

-- @see clone

function class(base)

-- __parent 属性缓存父类,便于子类索引父类方法

return setmetatable({__parent = base}, {

__call = _instantiate,

__index = base

})

end

--[[

local function searchParentClass(k, plist)

for i=1, #plist do

local v = plist[i][k]

if v then return v end

end

end

--- Create a Class object (Python-style object model).

-- The class object can be instantiated by calling itself.

-- Any class functions or shared parameters can be attached to this object.

-- Attaching a table to the class object makes this table shared between

-- all instances of this class. For object parameters use the __init__ function.

-- Classes can inherit member functions and values from a base class.

-- Class can be instantiated by calling them. All parameters will be passed

-- to the __init__ function of this class - if such a function exists.

-- The __init__ function must be used to set any object parameters that are not shared

-- with other objects of this class. Any return values will be ignored.

-- @param base The base class to inherit from (optional)

-- @return A class object

-- @see instanceof

-- @see clone

function class(...)

local mtb = {}

local parents = {...}

mtb = setmetatable(mtb, {

__call = _instantiate,

__index = function(t,k)

return searchParentClass(k, parents)

end})

mtb.__index = mtb

return mtb

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 instanceof(object, class)

local meta = getmetatable(object)

while meta and meta.__index do

if meta.__index == class then

return true

end

meta = getmetatable(meta.__index)

end

return false

end

说明:

1、 通过class来确定继承关系

2、 通过instanceof来判断继承关系

LUA OOP编程实现方法的更多相关文章

  1. js原生设计模式——4安全的工厂方法模式之oop编程增强版

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. PHP面向对象(OOP)编程入门教程

    面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行PHP的高级编程,对于提高 PHP编程能力和规划好Web开发构架都是非常有意义的.下面我们就 ...

  3. 【PHP面向对象(OOP)编程入门教程】1.什么是面向对象?

    面向对象编程(Object Oriented Programming, OOP, 面向对象程序设计)是一种计算机编程架构,OOP的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成 ...

  4. lua OOP实现对象的链式调用

    数学中的链式法则 http://sx.zxxk.com/ArticleInfo.aspx?InfoID=164649 链式微分法则:实数运算的链式法则:对数运算的链式法则:平行公理的链式法则:向量运算 ...

  5. 玩转cocos2d-x lua-binding, 实现c++与lua混合编程

    引言 城市精灵GO(http://csjl.teamtop3.com/)是一款基于cocos2d-x开发的LBS社交游戏, 通过真实地图的探索, 发现和抓捕隐匿于身边的野生精灵, 利用游戏中丰富的玩法 ...

  6. php面向对象(OOP)编程完全教程

    摘自:http://www.php-note.com/article/detail/41 面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行P ...

  7. JavaScript的OOP编程1

    首先要说的是,javascript其实是可以进行OOP编程的,其次javascript的OOP编程实现方式有多种,我写的这一种只是我测试过,可行的一种 version1 // 父类 function ...

  8. PHP面向对象(OOP)编程入门教程链接

    PHP官方学习OOP: http://php.net/manual/zh/oop5.intro.php 从其他博主学习:(以下链接来源: http://blog.snsgou.com/post-41. ...

  9. Lua 调用 Opencv 的方法

    Lua 调用 Opencv 的方法 最近想用 Lua 调用 Opencv 进行相关像素级操作,如:bitwise_and 或者 bitwise_or,从而完成图像 IoU 的计算. 那么,怎么用 Lu ...

随机推荐

  1. Codeforces Round #243 (Div. 2) C. Sereja and Swaps

    由于n比较小,直接暴力解决 #include <iostream> #include <vector> #include <algorithm> #include ...

  2. 《深入浅出Windows 10通用应用开发》

        <深入浅出Windows 10通用应用开发>采用Windows 10的SDK进行重新改版,整合了<深入浅出Windows Phone 8.1应用开发>和<深入解析 ...

  3. [深入浅出WP8.1(Runtime)]Socket编程之UDP协议

    13.3 Socket编程之UDP协议 UDP协议和TCP协议都是Socket编程的协议,但是与TCP协议不同,UDP协议并不提供超时重传,出错重传等功能,也就是说其是不可靠的协议.UDP适用于一次只 ...

  4. 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明

    13:40 2015/11/20 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明 关键点:现在qq邮箱开通smtp服务后会给你一个很长的独立新密码,发邮件配置中的密码需要 ...

  5. 【noiOJ】P1996

    1996:登山 查看 提交 统计 提问 总时间限制:  5000ms 内存限制:  131072kB 描述 五一到了,PKU-ACM队组织大家去登山观光,队员们发现山上一个有N个景点,并且决定按照顺序 ...

  6. Linux进程含义知多少

    理想情况下,您应该明白在您的系统中运行的每一个进程.要获得所有进程的列表,可以执行命令 ps -ef(POSIX 风格)或 ps ax(BSD 风格).进程名有方括号的是内核级的进程,执行辅助功能(比 ...

  7. DBLink创建 ORA-12154: TNS: 无法解析指定的连接标识符

    因为对oracle不了解,这个问题可TM的搞了好久! 走的弯路: 1. 在客服端的PLSQL连接工具上折腾,而不是在服务器的PLSQL解决 2. 配置的tnsnames.org文件在环境变量path( ...

  8. 关于 微软必应词典客户端(pc) 的案例分析

    第一部分 调研,评测 ●评测 bug one 在词典界面中搜完单词后,将鼠标移到英文例句上的单词时,会显示对应的中文翻译,而当移到短语时则不对应中文翻译. bug two 用orc强力取词,查询如上图 ...

  9. Set

    package lis0924; //生成器导包 import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...

  10. HTML5 Input 类型

    浏览器支持 Input type IE Firefox Opera Chrome Safari email No 4.0 9.0 10.0 No url No 4.0 9.0 10.0 No numb ...