美文网首页Lua
Lua模拟继承关系

Lua模拟继承关系

作者: 沧州宁少 | 来源:发表于2018-07-25 19:45 被阅读3次
  • 定义父类。

    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~

相关文章

  • Lua模拟继承关系

    定义父类。local MsgBaseCell = {}MsgBaseCell.backView = nilMsgB...

  • Lua单继承与多继承理解

    Lua不具有面向对象、继承等高级语言的属性,但是借助万能的table,仍然可以模拟实现面向对象继承的特性。这里先要...

  • Lua实现继承

    Lua元表使用 中的__index元方法可以实现面向对象和继承关系: lua中没有类的概念,只有table,但可以...

  • Lua面向对象之继承关系

    介绍演示

  • Lua 继承

    Aitin原创稿件,转载请注明出处!使用Lua 也很久了,这里写一点使用心得 Lua 继承的使用 主要用到的是 _...

  • Lua继承

    Lua中的元表查询操作 class方法详解 下面看一下基本使用,一个MyBase的基类,然后一个MyChildA的...

  • 2018-08-02

    lua实现继承,重载和多态(上) *讲到lua的继承等面向对象的实现,首先得讲一下lua中的几个元方法和元表. s...

  • Lua实现继承

    Lua实现继承 我们知道在Lua脚本语言中是没有对象这个概念的。但是Lua为我们提供了一堆的元方法,可以实现类的继...

  • Lua多重继承

    继承多重继承 一. 一种提高性能的方法:

  • Lua实现继承

    Lua元表使用[https://www.jianshu.com/p/ef03c9d33c67] 中的__index...

网友评论

    本文标题:Lua模拟继承关系

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