我的直觉告诉我,我犯了一个明显的错误,但它逃避了我。我试图通过使用以下示例代码来从指定的“父”对象中创建“子”对象。Lua和对象产卵
local Ad = {
__index = Ad,
__tostring = function(self) return self.msg end,
__concat = function(a, b) return tostring(a)..tostring(b) end,
Initialize = function(self, msg, wgt)
if msg and msg ~= "" then
wgt = wgt or 1
self.msg, self.wgt, self.ranLog = msg, wgt, {}
end
end,
Increase = function(self)
if self.wgt < 9 then self.wgt = self.wgt + 1 end
end,
Decrease = function(self)
if self.wgt > 1 then self.wgt = self.wgt - 1 end
end
}
local function new(src, ...)
local o = setmetatable({}, src)
if src.Initialize then o:Initialize(...) end
return o
end
local ad = new(Ad, "Test Message")
print("Object - "..ad)
for k, v in pairs(ad) do print("", k, v) end
ad:Increase()
print("Modified Object - "..ad)
for k, v in pairs(ad) do print("", k, v) end
所以这是逐字与试图调用方法初始化这是一个零值什么失败。鉴于在编辑之前我的示例代码确实有效,是否已经定义了表格广告?或者我在这里某处有语法错误?
- 编辑,以替换代码片段与该失败
当我替换实际的代码您的意见,这对我的作品。我认为你太简化了你的代码。 – 2013-03-01 22:20:04
好的,这里是(除了我将作为示例留下的变量名称)更多的表格以防万一这是问题。 – user1901873 2013-03-02 00:04:11