当前位置: 首页 > news >正文

Lua 类管理器

Lua 类管理器

-- ***** Class Manager 类管理*****‘local ClassManager = {}local this = ClassManagerfunction ClassManager.Class(className, ...)print('ClassManager::Class')--print(className)-- 构建类local cls = {__className = className}--print(cls)-- 父类集合local supers = {...}for _, super in pairs(supers) do-- 获取父类的类型local superType = type(super)--print(superType)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' thenassert(cls.__create == nil, string.format("class() - create class \"%s\" with more than one creating function",className))cls.__create = superelseif superType == 'table' thenif super['.isclass'] thenassert(cls.__create == nil,string.format("class() - create class \"%s\" with more than one creating function or native class",className));cls.__create = function() super:create() endelse-- 用来保存父类cls.__supers = cls.__supers or {}local dp = falsefor _, v in pairs(cls.__supers) doif v.__className == super.__className thendp = truebreakendend-- set first super pure lua class as class.superif not dp then-- 将父类中所有的对象(变量或者函数)拷贝到子类中cls.__supers[#cls.__supers + 1] = superif not cls.super thencls.super = superendendendelseerror(string.format("class() - create class \"%s\" with invalid super type",className), 0)endendcls.__index = clsif not cls.__supers or #cls.__supers == 1 thensetmetatable(cls, {__index = cls.super})else-- 设置cls的元表为supers中的父类setmetatable(cls, {__index = function(_, key)local supers = cls.__supersfor i=1, #supers dolocal super = supers[i]if super[key] then return super[key] endendend})end-- 添加默认构造函数if not cls.constructor thencls.constructor = function() endend-- new 方法构建类对象cls.new = function(...)-- 构建一个对象local instanceif cls.__create theninstance = cls.__create(...)elseinstance = {}end-- 设置对象的元表为当前类setmetatable(instance, cls)instance.class = clsinstance:constructor(...)return instanceendcls.create = function(_, ...)return cls.new(...)end-- 返回类return clsendlocal setmetatableindex = function(t, index)local mt = getmetatable(t)mt = mt or {}if not mt.__index thenmt.__index = indexsetmetatable(t, mt)elseif mt.__index ~= index thensetmetatableindex(mt, index)end
endreturn ClassManager

使用

local MyObject = require('MyObject')
local ClassManager = require('ClassManager')
local obj3 = ClassManager.Class('obj3', MyObject)
--print(obj3)
obj3:myFunc()
obj3:myFunc2()
http://www.lryc.cn/news/407134.html

相关文章:

  • 实现领域驱动设计(DDD)系列详解:领域模型的持久化
  • 配置sublime的中的C++编译器(.sublime-build),实现C++20
  • Android14 - 前台Service、图片选择器 、OpenJDK 17、其他适配
  • 数据恢复教程:如何从硬盘、SD存储卡、数码相机中恢复误删除数据。
  • 谷粒商城实战笔记-47-商品服务-API-三级分类-网关统一配置跨域
  • stm32平台为例的软件模拟时间,代替RTC调试
  • 《设计模式之美》读书笔记2
  • C++ STL set_difference 用法
  • 【基础算法总结】优先级队列
  • python-绝对值排序(赛氪OJ)
  • 成功者的几个好习惯,你具备了几个
  • centos中zabbix安装、卸载及遇到的问题
  • php编译安装
  • [K8S] K8S资源控制器Controller Manager(4)
  • C#,.NET常见算法
  • KubeSphere介绍及一键安装k8s
  • Spring 系列
  • 基于opencv[python]的人脸检测
  • 配置SSH公钥互信
  • WEB渗透Web突破篇-SQL注入(MSSQL)
  • DAY15
  • pytest结合allure-pytest插件生成测试报告
  • 详细解析用户提交咨询
  • UDP/TCP协议解析
  • 力扣94题(java语言)
  • JavaScript基础入门:构建动态Web世界的基石
  • 01-client-go
  • WebRTC QoS方法十三.2(Jitter延时的计算)
  • PHP进阶:前后端交互、cookie验证、sql与php
  • 优思学院|ANOVA方差分析是什么?如何用EXCEL进行计算?