gpt4 book ai didi

oop - 函数如何进入 Lua 中的表?

转载 作者:行者123 更新时间:2023-12-02 00:18:09 24 4
gpt4 key购买 nike

我正在阅读 chapter 16.4 of Programming in Lua在最后一个例子中我无法得到一件事:

  function newAccount (initialBalance)
local self = {
balance = initialBalance,
LIM = 10000.00,
}<p></p>

<pre><code> local extra = function ()
if self.balance > self.LIM then
return self.balance*0.10
else
return 0
end
end

local getBalance = function ()
return self.balance + self.extra() -- this line is the problematic one
end

...
</code></pre>

<p></p>
“额外”功能如何变成“self.extra”功能?!我没有看到任何使它附加到单独的“self”表的东西!

最佳答案

这在本书的第二版中已更正为

local getBalance = function ()
return self.balance + extra()
end

关于oop - 函数如何进入 Lua 中的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com