Stay Hungry,Stay Foolish!

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

posted @   lightsong  阅读(1111)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2014-08-04 XP下安装MAC OS虚拟系统
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示