cc.lua
local CMD = {} -- 定义两个函数 function CMD.func1() print("Function 1 called") end function CMD.func2() print("Function 2 called") end return CMD
test.lua
local functions = require "cc" -- 设置表的元表和__call元方法 setmetatable(functions, { __call = function(self, functionName) local func = self[functionName] if func then return func() else error("Function not found: " .. functionName) end end }) functions.func1() -- 使用字符串名字调用对应的函数 functionName = "func2" functions(functionName)
输出:
Function 1 called
Function 2 called