lua OOP实现对象的链式调用
数学中的链式法则
http://sx.zxxk.com/ArticleInfo.aspx?InfoID=164649
链式微分法则:
实数运算的链式法则:
对数运算的链式法则:
平行公理的链式法则:
向量运算的链式法则:
JS对象链式调用方法
http://stackoverflow.com/questions/15029309/how-to-write-jquery-chainable-functions-for-local-usinghttp://stackoverflow.com/questions/15029309/how-to-write-jquery-chainable-functions-for-local-using
ar obj = {test : function(){ alert("Y"); return this; },test2 : function(){ alert("2"); return this; } } obj.test().test2(); // And so on since it returns this
lua OOP实现
--
-- Class helper routines
--
-- Instantiates a class
local function _instantiate(class, ...)
local inst = setmetatable({__class=class}, {__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
--- 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
local ChainClass = class()
ChainClass.setFlag = function ( self, flag )
self.flag = flag
print("set flag = "..flag)
return self
end
ChainClass.setType = function ( self, type )
self.type = type
print("set type = "..type)
return self
end
ChainClass.printAttrs = function ( self, flag )
for k,v in pairs(self) do
print("name="..k.." value="..tostring(v))
end
return self
end
local chainObj = ChainClass()
chainObj:setFlag(1):setType(2):printAttrs()
转载于:https://www.cnblogs.com/lightsong/p/5738649.html
lua OOP实现对象的链式调用相关推荐
- javascript --- 对象的方式体验链式调用
将功能相近的方法写入同一个对象中,是一个很好的编程习惯,便于后期的维护和前期的开发. foo1 var fooObj = {foo1: function() {console.log('foo1'); ...
- Lombok链式调用,子类对象set父类属性,返回父类对象
前言 lombok相信大家都用过,没用过肯定也不会点进来,一直用着都很舒服.今但是天在码代码时,碰到了一个问题,卡了半天.问题是这样的:Lombok链式调用父类中的set属性时,返回的居然是一个父类对 ...
- jq的链式调用.end();
jq的链式调用.end(); 先上code <!DOCTYPE html> <html lang="en"> <head><meta ch ...
- [jQuery] jQuery是如何链式调用的?
[jQuery] jQuery是如何链式调用的? 返回对象本身即可,类似 obj = { func: function(){ return obj } }, 那么 obj.func().func() ...
- 实现同步请求_图解 Promise 实现原理(二)—— Promise 链式调用
摘要 很多同学在学习 Promise 时,知其然却不知其所以然,对其中的用法理解不了.本系列文章由浅入深逐步实现 Promise,并结合流程图.实例以及动画进行演示,达到深刻理解 Promise 用法 ...
- 彻底掌握 Promise-原生Promise的实现(二) Promise的链式调用
在 彻底掌握 Promise- 原生 Promise 的实现(一) 已经初步实现了 promise 的简易版本,这距离实现完整版本的 Promise 还有一段距离,下面我们将在简易版本的基础上增加 P ...
- 设计模式之建造者模式(含链式调用)
开始之前 大家都知道建造者模式是23种设计模式之一,其属于创建型模式,我们可以先看一下菜鸟教程对建造者模式的介绍. 建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂 ...
- Java8链式调用_java链式调用(转载http://www.cnblogs.com/quiet-snowy-day/p/6091233.html)
记录最近在项目设计中遇到的一个小问题. 前提:有这样两个POJO类,它们都可以通过链式调用的方式来设置其属性值,其中一个类继承了另一个类. 问题:通过链式调用,子类对象访问父类方法后,如何使返回对象仍 ...
- Android 快速实现新手引导层的库,通过简洁链式调用,一行代码实现引导层的显示
NewbieGuide 项目地址:huburt-Hu/NewbieGuide 简介:Android 快速实现新手引导层的库,通过简洁链式调用,一行代码实现引导层的显示 更多:作者 提 Bug 标 ...
最新文章
- java 常见中文乱码问题解决
- 鸿蒙智慧屏电视,鸿蒙os全面推送,立刻买了一台华为智慧屏电视s pro65
- RIM发警告 部分黑莓手机存在安全漏洞
- Javascript中call的使用
- elasticsearch id查询_互联网公司中对【Elasticsearch】的真实应用案例
- python struct pack解析_Python struct 详解
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
- 【计算机组成原理】数据存储的大端和小端
- 中值滤波器 ( Median Filter ) C++ 实现
- Union的一个妙法
- 原创 | 职场风云 (二)试验田
- SCSI设备IO过程:磁盘上线与IO过程
- Tkinter单选框
- C语言小熊时钟实验报告,用VC6.0编写一个小熊时钟(求代码)
- 数据库中索引原理及填充因子
- 电脑C盘满了怎么办?电脑C盘满了怎么清理?
- quorum examples初探
- Java 在PPT中添加文本水印的简易方法(单一/平铺水印)
- excel文件被写保护怎么解除_excel撤销写保护的教程
- PaperWork php源码,一个开源记笔记/存档的程序:Paperwork