首页 > 其他分享 >Lua 实现面向对象

Lua 实现面向对象

时间:2022-10-01 15:45:05浏览次数:46  
标签:.__ end 面向对象 实现 classname Lua super class cls

  function class(classname, super)
    assert(classname and classname ~= "","class name can not empyt")

    local v
    if GameDefince.UNITY_EDITOR then
        class_defines = class_defines of {}
        local _
        _,v = debug.getlocal(3, 1)
        if class_defines[classname] and class_defines[classname][v] then
             retrun class_defines[classname][v]
        end
     end

      local superType = type(super)
      local cls
      
      if superType == "function" and superType ~= "table" then
          superType = nil
          super =nil
      end

      if superType == "function" or (super and super.__ctype == 1) then
          cls = {}
          
          if superType == "table" then 
              for k,v in pairs(super)  do cls[k] = v end
              cls.__create = super.__create
              cls.super    = super
          else 
              cls.__create = super
          end
          
           cls.ctor  = function() end
          cls.__cname = classname
          cls.__ctype = 1

          function cls.new(...)
              local instance = cls.__create(...)

              for k,v in pairs(cls) do instance[k] = v end
              instance.class = cls
              instance:ctor(...)
              return instance
           end

        else
            if super  then
                cls = setmetatable({},super)
                cls.super = super
            else
                cls = { ctor = function() end;}
                function cls:uctor()
                     for k,v in pairs(self) do
                          self[k] = nil
                      end
                      self = nil;
                  end
            end
          cls.__cname = classname
          cls.__ctype = 2 -- lua
          cls.__index = cls

          function cls.new(...)
            local instance = setmetatabl({}, cls)
            instance.class = cls
            instance:ctor(...)
            return instance
          end
         end
        
      if GameDefine.UNITY_EDITOR then
          if not class_defines[classname] then
              class_defines[classname] = {}
          end
          class_defines[classname][v] = cls
      end
       
      return cls


标签:.__,end,面向对象,实现,classname,Lua,super,class,cls
From: https://www.cnblogs.com/zjh808/p/16700169.html

相关文章

  • mysql中的事务隔离级别序列化如何实现
    ......
  • 非递归的方式实现二分查找算法
    简介二分查找法的运行时间为对数时间O(㏒₂n),即查找到需要的目标位置最多只需要㏒₂n步,假设从[0,99]的队列(100个数,即n=100)中寻到目标数30,则需要查找步数为㏒₂100,即最......
  • 使用 NodeJS、Typescript 和 tsyringe 实现依赖倒置
    使用NodeJS、Typescript和tsyringe实现依赖倒置依赖倒置是5个SOLID原则之一,在我看来,也是最重要的原则之一,因为它允许通过抽象而不是使用具体实现来解耦模块。记......
  • 企业如何加快转型步伐,实现高效的数字化经营?
    在正确的道路上加快步伐能取得更早的成功,但选择错了,那是背道而驰!所以,进行数字化转型最重要的是认知自己,弄清楚自己未来发展方向与规划,明确自己的需求,然后才是选择适合自己......
  • 51单片机下实现软件模拟IIC通信
    1、IIC协议简易概述IIC全称Inter-IntegratedCircuit(集成电路总线),是由PHILIPS公司在80年代开发的两线式串行总线,用于连接微控制器及其外围设备。IIC属于半双工同步通信......
  • 怎样用NFC卡绑定具体用户或者物,从而实现刷卡后,根据NFC卡内信息自动触发客户端应用程序
    标题是个大命题,场景其实有很多很多,而且千变万化,一个帖子很难畅聊,以后会慢慢谈。说一个最常见的场景。每个员工发张NFC卡,管理者给每人分配的卡里写入账号和密码,从而实现刷卡......
  • win10安装mongoDB并实现远程连接
    我这里安装的是4.2mis版本1、下载(官网)[https://www.mongodb.com/try#community]2.安装前准备:建立mongoDB,作为安装目录:F:\mongoDB立data/db,用于存放数据:F:\Mong......
  • Python基本算法实现及总结归纳
    @目录冒泡排序快速排序插入排序选择排序希尔排序归并排序各个算法的时间复杂度附:二分法冒泡排序这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端(......
  • 如何实现 Token 无感刷新 All In One
    如何实现Token无感刷新AllInOne实现原理要实现无感刷新,接口一般会返回两个token:一个access_token用于授权验证一个refresh_token用于更新access_tokenrefs......
  • OLE实现Excel下载
    货铺QQ群号:834508274这里的Excel格式略微复杂:一般会用smw0先上传模板,然后客户下载时,打开模板,填充数据,最后下载到本地。本例使用上面的模板。下面是demo(用电脑版观看更合适~)......