Lua模拟继承关系

  • 定义父类。

    local MsgBaseCell = {}
    MsgBaseCell.backView = nil
    MsgBaseCell.responseTap = false
    
    -- base function
    function MsgBaseCell:new(o,contentView)
        o = o or {}
        setmetatable(o,self)
        self.__index = self
        self.responseTap = false
        self.backView = contentView or View()
        return o
    end
    
    function MsgBaseCell:init(cell)
        -- cell | contentView | other
        local bgColor = Color()
        bgColor:rgba(0,0,0,0.2)
        self.backView:bgColor(bgColor)
        self.backView:cornerRadius(12.0)
        local contentView = cell.contentView
        local array = Array(1)
        array:add(self.backView)
        contentView:flexChildren(array)
    end
    
    function MsgBaseCell:layoutSubViews(cell,item)
         local width = cell.contentView:width()
         local height = cell.contentView:height()
         self.backView:flexCss(string.format('position:absolute,top:%f,left:%f,width:%f,height:%f',0,0,width,height))
         cell.contentView:flxLayout()
    end
    
    return MsgBaseCell
    
  • 定义子类

    -- 获取父类对应的表名。此处为绝对路径。
    local MsgBaseCell = require("MsgBaseCell")
    -- 定义子类对应的临时表
    local MsgAttributeCell = {}
    MsgAttributeCell.attributeLabel = nil
    
    function MsgAttributeCell:new()
        -- 获取父类的一个实例(表对象)
        local objc = MsgBaseCell:new()
        -- 获取父类对应的元表
        local super_mt = getmetatable(objc)
        -- 设置self表对应的元表为父类的元表{__index = self}。这样形成一种递归的关系。因为self以后也是为了合成一张元表的。当其他的表在self表找不到的时候会递归去self对应的元表去查询有没有__index。发现__index对应一张表则会把key传递到这表继续查找。以此向上递归上去
        -- 以上关系是 1.当前表查询。找到完事,没找到继续  2.查询元表有没有__index。如果__index没有 终止。3.如果对应一张表则递归1.2直到找到或者递归完所有的元表
        setmetatable(self,super_mt)
        -- 这样设置后,可以通过self.super:method(...) 调用父类的已被覆盖的方法。
        -- 这一步的操作是设置objc的super指向一张空表{}。setmetatable这步会设置{}的元表为super_mt。并且会返回{}。当通过super去调用方法的时候
        -- 先会去self.super对应的表里面去查。此时 tableA =self.super。tableA没有相应的话则会去tableA对应的元表去查,结果找到则会去调用。没有则报方法找不到
        objc.super = setmetatable({},super_mt)
        -- 此时objc = {super= {}}。设置objc对应的元表为{__index = self}。而self表对应的元表关系上面已经映射好了。查询super的话。因为super表找不到,会去super表的元表找。上一步设置了super表的元表为super_mt{__index = self}。找到看存在__index字段则去查找比如init 找到则调用。 对于其他的方法。因为objc表找不到。则去原表查找(__index递归逻辑上面讲到),self表找到则调用。找不到则查找self对应的元表,上面已经设置。形成递归查找。
        return setmetatable(objc,{__index = self})
    end
    
    function MsgAttributeCell:init(cell)
        -- 相当于调用了super:init(cell)
        self.super:init(cell)
        self.attributeLabel = Label()
        local bgColor = Color():clear()
        self.attributeLabel:bgColor(bgColor)
        local textColor = Color():white()
        self.attributeLabel:textColor(textColor)
        self.attributeLabel:boldSystemSize(14.0)
        local contentView = cell.contentView
        local array = Array(1)
        array:add(self.attributeLabel)
        contentView:flexChildren(array)
    end
    
    function MsgAttributeCell:onLayout(cell,item)
         self:layoutSubViews(cell,item)
         self:updateData(item)
    end
    
    function MsgAttributeCell:layoutSubViews(cell,item)
        -- 相当于调用了super:layoutSubViews(cell,item)
        self.super:layoutSubViews(cell,item)
        local contentView = cell.contentView
        local width = contentView:width()
        self.attributeLabel:flexCss(string.format('margin-left:%f,margin-top:%f,width:%f,height:%f',7,1,width-34,20))
        contentView:flxLayout()
    end
    
    function MsgAttributeCell:updateData(item)
        self.attributeLabel:text(item.title)
    end
    
    return MsgAttributeCell
    

上面如果理解有问题或者细节有误请指出thanks~

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

推荐阅读更多精彩内容