美文网首页
【Lua】之面向对象,类的完整实现

【Lua】之面向对象,类的完整实现

作者: 水曜日鶏 | 来源:发表于2020-02-11 22:27 被阅读0次

Lua面向对象作为游戏面试时的高频考试题,属于必须会的知识点。以下内容提供了一个完整的类的创建模板。包括继承,函数重载,构造函数以及类的实例化。

  • 基类模板的创建,新建一个BassClass.lua存放下面内容。以后每定义一个新的类,这个脚本都不用更改
local _class = {}

local function Init(obj, ...)
    local create
    create = function(classtype, ...)
        if classtype.supper then
            create(classtype.supper, ...)
        end
        if classtype.__init then
            classtype.__init(obj, ...)
        end
    end

    create(obj._class_type, ...)
end

local function New(classtype,...)
    local obj = {}
    obj._class_type = classtype
    setmetatable(obj,{__index = _class[classtype]})
    _class[classtype]:Func()
    Init(obj,...)

    return obj
end

function BaseClass(classname,supperclass)
    local class_type = {}

    class_type.__init = false
    class_type.__cname = classname
    class_type.supper = supperclass
    class_type.New = function(...)
        return New(class_type, ...)
    end

    local vtbl = {}
    _class[class_type] = vtbl
    setmetatable(class_type, {
        __newindex = function(table, key, value)
            vtbl[key] = value
        end,
        __index = vtbl
    })

    if supperclass then
        setmetatable(vtbl, {
            __index = function(table, key)
                return _class[supperclass][key]
            end
        })
    end
    
    return class_type
end
  • 新类的定义。下面定义了class1和class2。
    __init是构造函数,Func是class1独有的方法。
    只需要一行代码就能表示class2继承自class1,非常方便。
require("BaseClass")

local class1 = BaseClass("Class1")

local function class1Init(self)
    print("class1Init")
    
end 

local function Func(self)
    print("class1Func")
end

class1.__init = class1Init

class1.Func = Func

local class2 = BaseClass("Class2",class1)

local function class2Init(self)
    print("class2Init")
end

class2.__init = class2Init
  • 类的实例化
-- result:
-- class1Init
-- class2Init

local class2Instance = class2.New()

-- result
-- class1Func
class2Instance:Func()
  • 构造class2的调用流程
image
  • _class、classtype和new出来的实例之间的关系


    image

相关文章

  • 【Lua】之面向对象,类的完整实现

    Lua面向对象作为游戏面试时的高频考试题,属于必须会的知识点。以下内容提供了一个完整的类的创建模板。包括继承,函数...

  • 大话C与Lua(五) 面向对象的数据结构——userdata

    如何实现面向对象? 熟悉Lua的同学都知道!在Lua内部已经实现了面向对象的基本机制(table), 同时也为宿主...

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

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

  • Lua实现继承

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

  • Lua面向对象实现

    这个类主要是把基类和派生类绑定起来,并且调用ctor构造函数用法如下 注意调用父类的方法要用"."别用":"是因为...

  • Lua实现面向对象

    注:只是仿照,并不是真正的面向对象C#中是用this表示当前类的对象,Lua中使用self点(.)与冒号(:)的区...

  • Lua 元表和元方法

    table 作为 Lua 中唯一的数据结构,我们可以利用 table 实现面向对象编程中的类、继承、多重继承等等。...

  • Lua基础知识(二)

    Lua中的面向对象 1.Lua中类的原理 Lua中没有类的概念,类是一个属性和方法的集合,而方法都是由方法名和方法...

  • 2018-08-02

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

  • lua实现多继承

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

网友评论

      本文标题:【Lua】之面向对象,类的完整实现

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