美文网首页
2018-08-06

2018-08-06

作者: AbnerTan | 来源:发表于2018-08-06 21:16 被阅读0次

    lua实现继承,重载和多态(下)


    上一篇讲了,lua的几个元方法和元表, 这里我们直接手动实现一个类方法, 可以创建类,并且能够在new出新的实例时自动调用类的构造函数ctor:

    local setmetatableindex_
    setmetatableindex_ = function(t, index)
        local mt = getmetatable(t)
        if not mt then
            -- if mt is nil, and index is table
            if type(index) == "table" and index.__index then
                setmetatable(t, index)
                return
            end
            mt = {}
        end
    end
    setmetatableindex = setmetatableindex_
    
    local function class_tostring(t)
        if t.__cname then
            return string.format("%s", t.__cname)
        end
        return tostring(t)
    end
    
    local function tostring_func(t)
        return string.format("%s: 0x%08X", class_tostring(t), t.__cid)
    end
    
    
    function class(className, ... )
        local cls = {
            __cname = className,
            __tostring = tostring_func,
        }
        local supers = {...} 
        for _, super in ipairs(supers) do
            local superType = type(super)
            if superType == "function" then
                cls.__create = super
            elseif superType == "table" then
                if super[".isclass"] then
                    cls.__create = function ()
                        return super:create()
                    end
                else
                    cls.__supers = cls.__supers or {}
                    cls.__supers[#cls.__supers + 1] = super
                    if not cls.__super then
                        -- set first super pure lua class as class.super
                        cls.__super = super
                    end
                end
            end
        end
    
        cls.__index = cls
        if not cls.__supers or #cls.__supers==1 then
            setmetatable(cls, {__index = cls.__super})
        else
            setmetatable(cls, {__index = function (_, key)
                local supers = cls.__supers
                for i = 1, #supers do
                    local super = supers[i]
                    if super[key] then return super[key] end
                end
            end})
        end
    
        if not cls.ctor then
            cls.ctor = function() end
        end
            
        cls.new = function(...)
            local instance
            if cls.__create then
                instance = cls.__create(...)
            else
                instance = {}
            end
            local ty = type(instance)
            local addr = tostring(instance)
            instance.__cid = tonumber(string.sub(addr, #ty+2), 16)
            instance.__class = cls
            setmetatableindex(instance, cls)
    
            local create
            create = function(c, ...)
                if rawget(c, "__super") then
                    create(c.__super, ...)
                end
                if rawget(c, "ctor") then
                    c.ctor(instance, ...)
                end
            end
            create(cls, ...)
            return instance
        end
    
        cls.create = function(_, ...)
            return cls.new(...)
        end
    
        return cls
    end
    
    
    -- @eg:
    -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    Animate = class("Animate")
    function Animate:ctor()
        print("Animate construct function!!!")
        self.tp = 5
    end
    
    function Animate:showName()
        print("normal Animate")
    end
    
    function Animate:setTempValue()
        self.temp = 666
    end
    
    Brid = class("Brid", Animate)
    function Brid:ctor(name, typ)
        self.name = name
        self.type = typ
        print("Brid construct function!!!", tostring(self) )
    end
    
    function Brid:showName()
        print("beautiful Brid!!!")
    end
    
    tt = {age = 12, func = function (...)
        print(...)
    end}
    td = class("td", tt)
    function td:ctor(name, typ)
        self.name = name
        self.type = typ
        print("td construct function!!!")
    end
    
    function td:showName()
        print("beautiful td!!!")
    end
    -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    bd = Brid.new("huahua", "maque")
    print(bd.tp, bd.temp)
    bd.tp = 10
    bd:setTempValue()
    print(bd.tp, bd.temp, bd.name, bd.type)
    bd:showName()
    
    bp = Brid.new("xiaohei", "wuya")
    print(bp.tp, bp.temp, bp.name, bp.type)
    print("tostring value --->>>", tostring(bd), tostring(bp))
    
    
    at = Animate.new()
    at:showName()
    print(at.tp, tostring(at))
    -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    print("*****************************************")
    dt = td.new("abner", "male")
    print(dt.age, dt.name, dt.type)
    dt.func("wonderful day!!!")
    

    我们简单的实现了lua中class的方法,不是很完善,但是麻雀虽小五脏俱全感兴趣的可以去查看下 tolua++实现的class源码,它是只是"userdate"的c类型,上面的实现仅仅只有支持lua类型
    class的实现思路其实就是巧妙的利用了 lua的元方法和元表的特性
    转载请写明出处:https://www.jianshu.com/p/41e768cb3ab9

    相关文章

      网友评论

          本文标题:2018-08-06

          本文链接:https://www.haomeiwen.com/subject/gdcnvftx.html