博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lua OOP实现对象的链式调用
阅读量:7221 次
发布时间:2019-06-29

本文共 2365 字,大约阅读时间需要 7 分钟。

数学中的链式法则

链式微分法则:

实数运算的链式法则:
对数运算的链式法则:
平行公理的链式法则:
向量运算的链式法则:

 

JS对象链式调用方法

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()

转载地址:http://idhym.baihongyu.com/

你可能感兴趣的文章
css3 Border属性
查看>>
基于vue的Element-ui定义自己的select组件
查看>>
Windows 10 Technical Preview 安装体验及变化
查看>>
Windows Server 2008 R2入门之FTP服务器
查看>>
USB 驱动架构浅析
查看>>
CSS定位元素居中显示
查看>>
Linux中用户和组中认证库和解析库的文件格式以及默认参数定义文件
查看>>
Windows中如何删除大量文件夹
查看>>
radio多次点击 选中与不选中
查看>>
21天让你成为Horizon View高手—Day19:Horizon View 5.2新功能—Html Ac
查看>>
netty初步认知
查看>>
redis
查看>>
用过的发送邮件的方法。
查看>>
VMWare
查看>>
【论文阅读】Image Super-Resolution Using Deep Convolutional Networks
查看>>
web.xml 中的listener、 filter、servlet 加载顺序及其详解
查看>>
try catch finally
查看>>
Windows编程之作业篇
查看>>
一文了解“Service Mesh(服务网格)”的历史与现在
查看>>
使用 rt_tables 巧妙配置 Linux centos7多网卡多路由实现策略路由
查看>>