Lua继承

作者: 咸鱼而已 | 来源:发表于2017-10-23 13:34 被阅读148次

Lua中的元表查询操作

1.在表中查找,如果找到,返回该元素,找不到则继续
2.判断该表是否有元表(操作指南),如果没有元表,返回nil,有元表则继续
3.判断元表(操作指南)中有没有关于索引失败的指南(即__index方法),如果没有(即__index方法为nil),则返回nil;如果__index方法是一个表,则重复1、2、3;如果__index方法是一个函数,则返回该函数的返回值

class方法详解

function class(classname, ...)
    local cls = {__cname = classname}

    local supers = {...} --lua中接收多个参数
    for _, super in ipairs(supers) do
        local superType = type(super)
        assert(superType == "nil" or superType == "table" or superType == "function",
            string.format("class() - create class \"%s\" with invalid super class type \"%s\"",
                classname, superType))

        if superType == "function" then
            assert(cls.__create == nil,
                string.format("class() - create class \"%s\" with more than one creating function",
                    classname));
            -- if super is function, set it to __create
            -- 这个是继承的C++的类
            cls.__create = super
        elseif superType == "table" then
            if super[".isclass"] then
                -- super is native class
                assert(cls.__create == nil,
                    string.format("class() - create class \"%s\" with more than one creating function or native class",
                        classname));
                -- 继承C++的类
                cls.__create = function() return super:create() end
            else
                -- super is pure lua class
                -- lua 的类
                cls.__supers = cls.__supers or {}
                cls.__supers[#cls.__supers + 1] = super
                if not cls.super then
                    -- 把继承的类赋值给cls.super
                    cls.super = super
                end
            end
        else
            error(string.format("class() - create class \"%s\" with invalid super type",
                        classname), 0)
        end
    end

    cls.__index = cls   -- 设置自己的__index 为自己
    if not cls.__supers or #cls.__supers == 1 then
        -- 如果只继承过来一个类,直接把元表的__index指向继承的类
        setmetatable(cls, {__index = cls.super})
    else
        -- 这里应该是根据不同的key从不同的类创建对象
        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(...)
        --创建对象,会调用自身的create方法(如果有的话),下边会调用ctor方法来初始化
        local instance
        if cls.__create then
            instance = cls.__create(...)
        else
            instance = {}
        end
        setmetatableindex(instance, cls)
        instance.class = cls
        instance:ctor(...)
        return instance
    end
    -- 上面说的create方法也会调用new进而调用ctor
    cls.create = function(_, ...)
        return cls.new(...)
    end
    return cls
end

下面看一下基本使用,一个MyBase的基类,然后一个MyChildA的派生类,看一下实现:

MyBase

local MyBase = class("MyBase")
function MyBase:ctor(id)
    self.__index = self;
    self.id = id or 100;
    self.num = 111
    print("MyBase ctor. .... ", self.id)
end
function MyBase:log()
    print("this is parent id: ",self.id)
end
return MyBase

MyBaseA

local MyBase = require("src/app/views/MyBase")
local MyChildA = class("MyChildA", MyBase)

function MyChildA:ctor(id)
    MyChildA.super.ctor(self, id) --调用父类方法
    print("MyChildA ctor....")
end

function MyChildA:log(  )
    MyChildA.super.log(self)    --调用父类方法
    print("this is MyChildA id: ", self.id)
end

return MyChildA

相关文章

  • Lua 继承

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

  • Lua继承

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

  • 2018-08-02

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

  • Lua实现继承

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

  • Lua实现继承

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

  • Lua多重继承

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

  • Lua实现继承

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

  • 对lua中类、实例、继承的理解

    lua底层有__index等表,去实现类,实例、继承。但是比较绕。 参考1.1.5.Lua语言面向对象之复制表的方...

  • Lua模拟继承关系

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

  • lua实现多继承

    lua对于面向对象的支持主要通过table来实现,每个table都是一个对象,对于继承,lua有元表的机制,通过s...

网友评论

      本文标题:Lua继承

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