2013-03-27 18 views
0

我正在嘗試學習Lua,所以希望這是一個很容易回答的問題。以下代碼不起作用。變量childContext在類的所有實例之間泄漏。 Lua對象表在對象實例之間泄漏,但不是記錄

--[[-- 
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不?

回答

1
  1. 不,我不這麼認爲。您希望您創建的每個子Context對象都具有其自己的childContexts字段。

  2. 它可以工作,因爲您將包含名稱字段的表傳遞給New函數。什麼,你在這裏做的:

CONTEXT1 = CxBR_Context:新{名稱= 「CONTEXT1」}

  • 創建 「name」 字段的表設置爲 「CONTEXT1」
  • 傳遞表到構造函數
  • [在構造函數中]爲您在第一步中創建的表分配一個metatable(原始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"} 

如果你的對象參數是零它只會工作,也就是說,如果您僅使用自參數調用構造函數。

+0

感謝您對構造函數工作原理的解釋。對於我,這說得通。不過,我對你的編輯有點困惑。爲什麼「對象或{childContext = {}}」不起作用?它似乎按預期工作。 – 2013-03-27 20:54:09

+0

是的,它可能會讓人困惑。你有兩個地方,你可以調用New方法:'Context1 = CxBR_Context:New {name =「Context1」}' '和context1 = Context1:New()'。在第一種情況下,由於您將自己的表提供給構造函數,因此Context1仍然與CxBR_Context共享childContext。只有在第二個實例中,context1確實有它自己的childContexts表。這可能是你想要的,所以請不要理我的編輯。 – 2013-03-28 15:57:41

+0

謝謝你解釋W.B. – 2013-03-29 04:09:00

0

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