2018-08-06

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的元方法和元表的特性
转载请写明出处://www.greatytc.com/p/41e768cb3ab9

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (2018春节自由行自驾游活动) 我们相约汝城过大年! 汝城县境内山水清嘉,景色秀丽,有...
    有我有你1122阅读 1,098评论 0 0
  • 学习Kotlin有十来天了,赶觉还是蛮爽的,特别是在接触lambda表达式后,当然Java8后也支持了这些方式。 ...
    柯基爱蹦跶阅读 9,465评论 3 5
  • title: JsBridge设计和规范 版 本 历 史 1. 前言 混合开发最大的优势,在于页面的热更新。而混合...
    海南鸡阅读 2,048评论 0 1
  • #AFNetworking源码阅读系列 一 前言: AFNetWorking一款轻量级网络请求开源框架,基于iOS...
    Xcode_破晓阅读 345评论 0 0
  • 附录 std 命名空间是所有C++ 标准库的栖身处 然而C 标准程序库耶适用于C++ 内。 signature 签...
    罗兆峰阅读 277评论 0 0