【问题标题】:Lua objects tables leaking between object instances, but not recordsLua对象表在对象实例之间泄漏,但不是记录
【发布时间】:2013-03-27 06:59:24
【问题描述】:

我正在尝试学习 Lua,所以希望这是一个容易回答的问题。以下代码不起作用。变量 childContext 在类的所有实例之间泄漏。

--[[--
Context class.
--]]--
CxBR_Context = {}

-- Name of Context
CxBR_Context.name = "Context Name Not Set"

-- Child Context List
CxBR_Context.childContexts = {}

-- Create a new instance of a context class
function CxBR_Context:New (object)
  object = object or {} -- create object if user does not provide one
  setmetatable(object, self)
  self.__index = self
  return object
end

-- Add Child Context
function CxBR_Context:AddChildContext(context)
  table.insert(self.childContexts, context)
  print("Add Child Context " .. context.name .. " to " .. self.name)
end


--[[--
Context 1 class. Inherits CxBR_Context
--]]--
Context1 = CxBR_Context:New{name = "Context1"}


--[[--
Context 1A class.Inherits CxBR_Context
--]]--
Context1A = CxBR_Context:New{name = "Context1A"}


--[[--
Context 2 class.Inherits CxBR_Context
--]]--
Context2 = CxBR_Context:New{name = "Context2"}


--[[--
TEST
--]]--
context1  = Context1:New() -- Create instance of Context 1 class
print(context1.name .." has " .. table.getn(context1.childContexts) .. " children")
context2  = Context2:New() -- Create instance of Context 2 class
print(context2.name .." has " .. table.getn(context2.childContexts) .. " children")
context1A = Context1A:New() -- Create instance of Context 1A class
print(context1A.name .." has " .. table.getn(context1A.childContexts) .. " children")

context1:AddChildContext(context1A) -- Add  Context 1A as child to context 1

print(context1.name .." has " .. table.getn(context1.childContexts) .. " children") -- Results Okay, has 1 child
print(context2.name .." has " .. table.getn(context2.childContexts) .. " children") -- Why does thin return 1, should be 0

查看Object oriented lua classes leaking,我可以通过将构造函数更改为:

-- Child Context List
-- CxBR_Context.childContexts = {}

-- Create a new instance of a context class
function CxBR_Context:New (object)
  object = object or { childContexts = {} } -- create object if user does not provide one
  setmetatable(object, self)
  self.__index = self
  return object
end

所以我的问题是:

  1. 有没有一种更简洁的方式来声明类变量,就像 第一个例子,所以我不必将它包含在构造函数中?
  2. 为什么 CxBR_Context.name 有效,但表 CxBR_Context.childContexts 没有?

【问题讨论】:

    标签: oop lua lua-table metatable


    【解决方案1】:
    1. 不,我不这么认为。您希望您创建的每个子 Context 对象都有自己的 childContexts 字段。

    2. 它有效,因为您将包含名称字段的表传递给 New 函数。你在这里所做的是:

    Context1 = CxBR_Context:New{name = "Context1"}

    • 创建一个将字段“name”设置为“Context1”的表
    • 将表传递给构造函数
    • [在构造函数中] 将元表(原始 CxBR_Context 表)分配给您在第一步中创建的表。

    所以基本上,当您调用 Context1.name 时,您会从调用构造函数时创建的 Context1 表中检索一个字段。但是当你用 childContext 索引它并且不存在这样的字段时(因为在构造函数阶段你只创建了一个“名称”字段),Lua 会查找 __index 表,即 CxBR_Context。而且这个表对所有的 ContextX 对象都是通用的。

    编辑:

    object = object or { childContexts = {} } -- create object if user does not provide one
    

    实际上,如果您像这样提供自己的表格,这也行不通:

    Context1 = CxBR_Context:New{name = "Context1"}
    

    只有当你的对象参数为 nil 时,它才会起作用,也就是说,如果你只使用 self 参数调用构造函数。

    【讨论】:

    • 感谢您对构造函数如何工作的解释。对于我,这说得通。但是,我对您的编辑有点困惑。为什么“object or { childContext={} }”不起作用?它似乎按预期工作。
    • 是的,它可能让人感到困惑。您有两个地方可以调用 New 方法:Context1 = CxBR_Context:New{name = "Context1"}context1 = Context1:New()。在第一个实例中,Context1 仍然与 CxBR_Context 共享 childContexts,因为您向构造函数提供了自己的表。只有在第二种情况下 context1 确实有自己的 childContexts 表。这可能是你想要的,所以请忽略我的编辑。
    • 谢谢W.B.的解释
    【解决方案2】:

    1.
    通过使用table.insert(self.member, v),您正在修改self.member 指向的容器的内容,该容器原来是该成员所在的最近超类的成员。
    如果您需要为子类的成员赋值,请明确执行。使用

    -- create a copy of array with one additional element
    self.childContexts = { context, unpack(self.childContexts) }
    

    而不是

    table.insert(self.childContexts, context)
    

    2.
    因为您正在为CxBR_Context.name 使用分配,而不是为CxBR_Context.childContexts 使用它。

    【讨论】:

      猜你喜欢
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 2012-10-11
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多