所以我正在为Lua创建一个系统,所以我可以在其中包含类和对象。我创建了对象,问题是创建构造函数。
我有这样的构造函数:
a = MyClass:Create("Hello World!")
create方法将...作为参数传递给构造函数方法(OnStart)。我可以在Create方法中读取参数,但是当调用OnStart时,参数最终会变为nil而不是“Hello World!”
我的代码:
Object = { }
function Object:Create(...)
local instance = { }
setmetatable(instance, self)
self.__index = self
instance.Type = Object
-- Now we can call the constructor.
local arg = { ... }
instance.OnStart(table.unpack(arg))
return instance
end
function Object:OnStart(msg)
print(msg)
end
test = Object:Create("Hello World!")
print(test:ToString())
在这里,msg参数的最终结果是nil ......
答案 0 :(得分:1)
我似乎发现了为什么它不起作用,需要更改的细节是一行9.而不是instance.OnStart
它必须是instance:OnStart
。